Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '*args,': 0.07; 'args)': 0.07; 'arguments': 0.07; 'collections': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "wouldn't": 0.11; '**kwds):': 0.16; '__new__': 0.16; '__slots__': 0.16; 'args:': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'design?': 0.16; 'guessing': 0.16; 'namedtuple': 0.16; 'namedtuples': 0.16; 'rationale': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.17; 'instance': 0.17; '>>>': 0.18; 'import': 0.21; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; "haven't": 0.23; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'interface': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; '>>>>': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'point': 0.31; 'url:python': 0.32; 'could': 0.32; 'docs': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'point.': 0.33; 'similar': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'signature': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'john': 0.60; 'different': 0.63; 'skip:n 10': 0.63; '2013': 0.84; 'cater': 0.84; 'oscar': 0.84; 'reid': 0.84; 'sfxlen:4': 0.84; 'pfxlen:big': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: John Reid Subject: Re: Differences creating tuples and collections.namedtuples Date: Mon, 18 Feb 2013 15:07:00 +0000 References: <7a40a426-baa9-46f8-8f9d-59ba32b044f3@googlegroups.com> <51221A6C.3030804@davea.name> <5122360C.20603@mail.cryst.bbk.ac.uk> <51223946.2020606@mail.cryst.bbk.ac.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Cc: Python List X-Gmane-NNTP-Posting-Host: cpc6-dals15-2-0-cust115.hari.cable.virginmedia.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 In-Reply-To: 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361200036 news.xs4all.nl 6858 [2001:888:2000:d::a6]:46726 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39100 On 18/02/13 14:53, Oscar Benjamin wrote: > On 18 February 2013 14:23, John Reid wrote: > [snip] >> That said it would be nice to know the rationale for >> namedtuple.__new__ to have a different signature to tuple.__new__. I'm >> guessing namedtuple._make has a similar interface to tuple.__new__. Does >> anyone know what the rationale was for this design? > > Because namedtuples use names for the arguments in the constructor: > >>>> from collections import namedtuple >>>> Point = namedtuple('Point', 'x y') >>>> p1 = Point(x=2, y=3) >>>> p1 > Point(x=2, y=3) >>>> p1.x > 2 > That's a good point. I haven't used __new__ much before but wouldn't something like this __new__() cater for both uses? (Example taken from namedtuple docs http://docs.python.org/2/library/collections.html#collections.namedtuple). >>> Point = namedtuple('Point', ['x', 'y'], verbose=True) class Point(tuple): 'Point(x, y)' __slots__ = () _fields = ('x', 'y') def __new__(_cls, *args, **kwds): 'Create a new instance of Point(x, y)' if args: return _tuple.__new__(_cls, args) else: return _tuple.__new__(_cls, (kwds[f] for f in _fields)) ... Perhaps I could subclass namedtuple so that my namedtuples have the correct signature for __new__.