[Python-Dev] Py2.6 ideas

Michele Simionato michele.simionato at gmail.com
Tue Feb 20 09:52:53 CET 2007


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 [2]: Point = NamedTuple('Point','x','y')

In [3]: Point(1,2)
Out[3]: Point(x=1, y=2)

In [4]: Point.__repr__ = tuple.__repr__

In [5]: Point(1,2)
Out[5]: (1, 2)

It feels clearer to me.

      Michele Simionato



More information about the Python-Dev mailing list