On 18/02/13 14:53, Oscar Benjamin wrote: > On 18 February 2013 14:23, John Reid <j.r...@mail.cryst.bbk.ac.uk> 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__. -- http://mail.python.org/mailman/listinfo/python-list