Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.04; 'method.': 0.05; 'modified': 0.05; 'args)': 0.07; 'arguments': 0.07; '*args):': 0.09; '__init__': 0.09; 'os.path': 0.09; 'overridden': 0.09; 'wrong,': 0.09; 'def': 0.10; 'sat,': 0.15; '__new__': 0.16; 'attaching': 0.16; 'inheritance': 0.16; 'instead:': 0.16; 'wrote:': 0.17; 'exists': 0.17; 'all,': 0.21; 'import': 0.21; 'example': 0.23; 'class.': 0.23; 'pass': 0.25; 'header:In-Reply- To:1': 0.25; 'creating': 0.26; 'am,': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'subject:list': 0.28; 'objects': 0.29; 'included': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'code': 0.31; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'expected': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'list.': 0.35; 'really': 0.36; 'but': 0.36; 'method': 0.36; 'operating': 0.36; 'possible': 0.37; 'itself': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'customized': 0.64; 'here': 0.65; 'taking': 0.65; 'want,': 0.65; 'receive': 0.71; 'subject:this': 0.84; '2013': 0.84; 'to:name:python': 0.84; 'redefining': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=y9/y8TpVp9TWnBlfZu2dJHbD2TmtVcBN2CAsq/BlPAU=; b=HJ23zwOgiKENee+GPET3I2KvW1MyzovNJ70bgAWmpBiKaPy8BSfOXbyrvVIIZakKPH tg/APeY5rnhR4PwItWKmhVJir0A5kyAsGv5fHQ7bHzwSDSI0JKH6K9D7H7FvCP5UJf41 vfJ6VvrSQEQ5Q8RrpjetD8fYCo8oJzVli3C9vg5dmxOzcOqfTO8QjmRhl7Jf8uRiWvd+ D9DvqXTkZy/I9+/Tw6qJHAs9z5pWLD8FBzISn8VPs5k53em5t9VZiGukPADDCaGKUJ6s 3/O1FgziSUEJCIhG625fPmhQVFgjPVsBbGv9cOloxGmo9EPg6eCfHAJG4G0f9OcR3dya oeMA== X-Received: by 10.52.22.194 with SMTP id g2mr4927316vdf.91.1362244989201; Sat, 02 Mar 2013 09:23:09 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Sat, 2 Mar 2013 10:22:29 -0700 Subject: Re: Is it correct this way to inherit from a list? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362244998 news.xs4all.nl 6850 [2001:888:2000:d::a6]:48997 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40340 On Sat, Mar 2, 2013 at 10:02 AM, gialloporpora wrote: > Hi all, > I would like to inherit from the list native class. > really I expected that was possible to use native list method without > redefining them, for example the __repr__ method. > > I don't know if i have made something wrong, this is my code (I obmit > customized methods that I have added): > > from os.path import exists > > class vector(list): > def __init__(self, *args): > self._list = list(args) So here you have a list subclass, but instead of taking advantage of that is-a relationship, you're creating a secondary list from the arguments and attaching it to self._list in a has-a relationship. The net effect is that you actually have two separate list objects here, with some methods operating on the list itself and some operating on the attached list. Try this instead: class Vector(list): def __new__(cls, *args): return super(Vector, cls).__new__(cls, args) def __init__(self, *args): super(Vector, self).__init__(args) The __new__ method here will receive the args in the style that you want, and then pass them up the inheritance chain to the superclass constructor, which will then just do the right thing. The __init__ method is also overridden to match the modified argspec. The super().__init__() call is included for completeness; AFAIK it doesn't actually do anything.