(begginer) issue with pickling class objects

François Granger francois.granger at free.fr
Mon Apr 9 16:12:43 EDT 2001


Steve Holden <sholden at holdenweb.com> wrote:

> "François Granger" <francois.granger at free.fr> wrote ...
> The problem here is that the objects you are pickling are instances of
> classes defined in your main module. When you call pickle.load() Python
> tries to re-import the class definition from its original module, but it
> cannot. The doumentation specifically states that the class of pickled
> objects must be importable, and the easiest way to arrange that is to put
> the class definitions in a separate module.

I had a vague idea about this but I did not made a test on it...

> It isn't terribly explicit in the documentation, so it isn't surprising. If
> you have a copy of Hammond & Robinson's "Win32 Programming in Python" then
> there's a good explanation in that book.

Sorry, I am working on a Mac ;-)

> > On a similar track, I would be happy to see a more general algorithm to
> > implement persistence of complexe objects between runs.
> >
> I believe Andrew Kuchling's ZODB implementation, as used in ZOPE, might be
> of interest to you. Andrew recently broke out a separate implementation,

I saw some messages about this, but never went up to verify if I could
use it...
I just went to download it now and i'll check.

> Unless you have more devious later applications in mind, it would seem more
> natural to simply generate a list of instances. I have recast your code into
> two modules, using lists rather than dictionaries, and added a __repr__
> method to the B class to allow for easy verification.

I removed lots of code. I need to uniquely identify instances of class A
object in class B. That is the reason why I used mappings instead of
lists.

> Seems to work OK. Hope this helps.

A lot.

I'll study your code to improve mine.

I may post some more complete code sometime ;-)

-- 
[fr, en, es, ia]
Information sur la hiérarchie usenet europa.* :
http://europa.usenet.eu.org



More information about the Python-list mailing list