Raymond Hettinger <python <at> rcn.com> writes:
More thoughts on named tuples after trying-out all of Michele's suggestions:
- The lowercase 'namedtuple' seemed right only because it's a function, but
as a factory function, it is somewhat class-like. In use, 'NamedTuple' more closely matches my mental picture of what is happening and distinguishes what it does from the other two entries in collections, 'deque' and 'defaultdict' which are used to create instances instead of new types.
This is debatable. I remember Guido using lowercase for metaclasses in the famous descrintro essay. I still like more the lowercase for class factories. But I will not fight on this ;)
- I remembered why the __repr__ function had a 'show' argument. I've
changed the name now to make it more clear and added a docstring. The idea was the some use cases require that the repr exactly match the default style for tuples and the optional argument allowed for that possiblity with almost no performance hit.
But what about simply changing the __repr__?
In : Point = NamedTuple('Point','x','y')
In : Point(1,2) Out: Point(x=1, y=2)
In : Point.__repr__ = tuple.__repr__
In : Point(1,2) Out: (1, 2)
It feels clearer to me.