Earlier today I had problems finding a namedtuple instance. As a health check, I tried to run the code that was sent in another answer . Here it is simplified a little more:
from collections import namedtuple import pickle P = namedtuple("P", "one two three four") def pickle_test(): abe = P("abraham", "lincoln", "vampire", "hunter") f = open('abe.pickle', 'w') pickle.dump(abe, f) f.close() pickle_test()
Then I changed two lines of this to use my named tuple:
from collections import namedtuple import pickle P = namedtuple("my_typename", "ABC") def pickle_test(): abe = P("ONE", "TWO", "THREE") f = open('abe.pickle', 'w') pickle.dump(abe, f) f.close() pickle_test()
However it gave me an error
File "/path/to/anaconda/lib/python2.7/pickle.py", line 748, in save_global (obj, module, name)) pickle.PicklingError: Can't pickle <class '__main__.my_typename'>: it not found as __main__.my_typename
i.e. the Pickle module is looking for my_typename . I changed the line P = namedtuple("my_typename", "ABC") to P = namedtuple("P", "ABC") , and it worked.
I looked at the source namedtuple.py , and in the end we have something that looks relevant, but I do not quite understand what is happening:
So my question is what exactly is going on? Why should the typename argument match the factory name for this?