[Python-ideas] namedtuple is not as good as it should be

Raymond Hettinger raymond.hettinger at gmail.com
Mon Jun 10 06:06:07 CEST 2013


On Jun 9, 2013, at 5:55 PM, Christian Tismer <tismer at stackless.com> wrote:

> If it is necessary to have class instances like today, ok. But there is no
> need to search that class in a pickle! Instead, the defining set of attributes
> could be pickled (uniquely stored by tuple comparison), and the class could
> be re-created on-the-fly at unpickling time.

That is a reasonable wish :-)
But, I'm not sure how you propose for it to work.
What would you expect from:

>>> Soldier = namedtuple('Soldier', ['name', 'rank', 'serial_number'])
>>> johnny = Soldier('John Doe', 'Private', 12345)
>>> roger = Soldier('Roger', 'Corporal', 67890)
>>> fireteam = [johnny, roger]
>>> pickletools.dis(pickle.dumps(fireteam))

Would it re-specify the class for every instance (at a cost of both speed and space)?

   [(nametuple, 'Soldier', ['name', 'rank', 'serial_number'], ('John Doe', 'Private', 12345)),
    (nametuple, 'Soldier', ['name', 'rank', 'serial_number'], ('Roger', 'Corporal', 67890))]

Or would you have a mechanism to specify the names just once?

   [(nametuple, 'Soldier', ['name', 'rank', 'serial_number'])
    (Soldier, ('John Doe', 'Private', 12345)),
    (Soldier, ('Roger', 'Corporal', 67890))]

What would you do with a customized named tuples?

>>> class PrettySoldier(Soldier):
        def __repr__(self):
              return 'N:{0} R:{1} S:{2}'.format(*self)

How about enumerations?  To pickle the value, Color.red, would you propose
for that instance to store the definition of the enumeration as well as its value?



Raymond


P.S.  This was supposed to be part of my previous email, but I had to board a flight.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130609/80f40ea8/attachment.html>


More information about the Python-ideas mailing list