Author alegonz
Recipients alegonz, docs@python
Date 2020-11-15.09:14:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1605431665.76.0.57179948955.issue42360@roundup.psfhosted.org>
In-reply-to
Content
I think the namedtuple documentation should mention that, for classes created with it to be pickle-able, the typename argument must match the name of the variable the class is being assigned to. Otherwise you get an error like this:

----------------------------------------------
>>> Foo = namedtuple("Bar", "x,y")
>>> pickle.dumps(Foo(1, 2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class '__main__.Bar'>: attribute lookup Bar on __main__ failed
----------------------------------------------

While it is indeed odd to do such naming in the first place, it should be admissible in other circumstances not involving pickling, and it is not obvious that pickling won't work if you do so.

The pickle documentation does mention this, though:

[...]Similarly, classes are pickled by named reference, so the same restrictions in the unpickling environment apply.[...]

but for someone who encounters this error it might be difficult to figure it out from that passage.

The added documentation in namedtuple could include a pointer to that section of pickle's documentation.
History
Date User Action Args
2020-11-15 09:14:25alegonzsetrecipients: + alegonz, docs@python
2020-11-15 09:14:25alegonzsetmessageid: <1605431665.76.0.57179948955.issue42360@roundup.psfhosted.org>
2020-11-15 09:14:25alegonzlinkissue42360 messages
2020-11-15 09:14:25alegonzcreate