Trying to understand pickle.loads withou class declaration

P

Philippe C. Martin

Hi,

Shuffling files around in my project, I notice I broke everything when I
stopped declaring classes in a program that "pickled.loaded" existing
pickled object of type "classes".

The error message being that the class(es) was unknown.

However, I _think_, I did manage to do the following in the past:

1) pickle.dump class "test" with field "A" intialized in declaration
2) pickle.load class "test" and add field "B" then pickle.dumps it
3) pickl.load class "test" with no problem although field "B" was not
initialized in that declaration.


I have two questions:

1) Why cannot the interpreter accept to load an object which class is not
defined ?
2) assuming the above (.... I _think_ ....) is true, why then is there not
problem in loading an object which changes dynamically.

Hope I'm making sense.

Any clue ?

Thanks,

Philippe
 
P

Philippe C. Martin

I confirm that all I have to do in order to successfully load a pickled
object of class A is to declare

class A:
def __init__(self):
pass

Although the object has tons of fields


Quid ?

Regards,

Philippe
 
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

Even

class A:
pass

should do the trick. Only the instance attributes are saved by a
pickle,
not the methods or the class itself. The unpickler tries to merge the
saved data and the class/method info that is not saved to recreate
the fully functional instance... but of course this info should still
be available. The unpickler will even import the class from the
appropriate module if necessary.

If you want your unpickler to manufacture a class for you when
it is not available, you may use the gnosis xml pickler with the
appropriate security ("paranoia") level.

http://gnosis.cx/download/gnosis/xml/pickle/

Cheers,

Seb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top