Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'args': 0.04; 'method.': 0.05; 'modified': 0.05; 'args)': 0.07; 'testing,': 0.07; '*args):': 0.09; '__init__': 0.09; 'overridden': 0.09; 'def': 0.10; 'sat,': 0.15; '__new__': 0.16; 'inheritance': 0.16; 'initializes': 0.16; 'need:': 0.16; 'wrote:': 0.17; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'subject:list': 0.28; 'included': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'that.': 0.30; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'method': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'further': 0.61; 'here': 0.65; 'want,': 0.65; 'receive': 0.71; 'subject:this': 0.84; '2013': 0.84; 'contents,': 0.84; 'to:name:python': 0.84 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=Hyy1KgLHFzX9Sl+cLL+2zDjzWPyVTrCWCoq5L8Cn5eI=; b=ojcdyfuTWRfo3FWPrQSUtgqfLHxodhwMtssLwsqZDnqKgiR/b/i37kh4pSJJXekUnf D8O0yxU+Y9ky+REGj4vBzDYoCnDJb8aJX3z4gkoRD1U30NGXHmJOmUrKcBmaqx0q2jBP lRSYl6iJjhFgyqhPi5Gy4OdBFSY1ljomuUDYq3wJsdrISQEKbZlVbFuG6TbInJUJM+s5 PNgoFpaaCQjcB2JLRXBf9bNJywRd428jBwNkELMi9Pb/Tg+Ol4M7HIXwDioANaMdo9sc nGnSz2t0G0q8XfYqmLqVaTH4voch0xr/2A3p79pyLJ03aYhD+xWsYGJjG79cnsb9Lkyr zarA== X-Received: by 10.52.177.163 with SMTP id cr3mr4905565vdc.94.1362245244258; Sat, 02 Mar 2013 09:27:24 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Sat, 2 Mar 2013 10:26:44 -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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362245252 news.xs4all.nl 6876 [2001:888:2000:d::a6]:53488 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40341 On Sat, Mar 2, 2013 at 10:22 AM, Ian Kelly wrote: > 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. I retract that. On further testing, it is actually the __init__ method that initializes the list contents, not the __new__ method. So this is all you need: class Vector(list): def __init__(self, *args): super(Vector, self).__init__(args)