New submission from Alejandro Gonzalez <alejandro...@gmail.com>:

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.

----------
assignee: docs@python
components: Documentation
messages: 381007
nosy: alegonz, docs@python
priority: normal
severity: normal
status: open
title: In the namedtuple documentation, mention that typename should match the 
variable name for the class to be pickle-able
type: enhancement

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42360>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to