Trying to understand pickle.loads withou class declaration

Philippe C. Martin philippe at philippecmartin.com
Sat May 28 14:57:19 EDT 2005


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





Philippe C. Martin wrote:

> 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




More information about the Python-list mailing list