When you define a class in a script, and then pickle instances of that class in
the same script and store them to disk, you can't load that pickle in another
script. At least not the straightforward way
[pickle.load(file('somefile.pickle'))]. If you try it, you get an
AttributeError during the unpickling operation.
There is no problem, of course, if the class is defined in a module which is
imported by the pickling script. pickle.load(file('somefile.pickle')) then
works.
Rather than provide specific examples here, there's a blog post from 2005 that
discusses this issue in depth and presents the problem very well:
http://stefaanlippens.net/pickleproblem. (I tested in Python 2.6 yesterday and
the same issue persists.)
Questions:
1) Does this have to be the case, or is it a design problem with pickles that
should be remedied?
2) Is there an easier way around it than moving the class definition to a
separate module? The blog post I point to above suggests putting "__module__ =
os.path.splitext(os.path.basename(__file__))[0]" into the class definiton, but
that's not working in my testing because when I do that, the pickling operation
fails. Is there something else that can be done?
This is obviously not a huge problem. Substantial classes should usually be
defined in a separate module anyway. But sometimes it makes sense for a script
to define a really simple, small class to hold some data, and needing to create
a separate module just to contain such a class can be a little annoying.
--
Gary Robinson
CTO
Emergent Music, LLC
personal email: [email protected]
work email: [email protected]
Company: http://www.flyfi.com
Blog: http://www.garyrobinson.net
--
http://mail.python.org/mailman/listinfo/python-list