Tuesday, August 9, 2011

A dangerous Pitfall in using cPickle + import

 

Assuming you have a module : myfunc.py

from  defunc_single import *

And defunc_single have a class that is  :

class Hello :

       def __init__ (self) :

               self.name = “hello”

Now if you access this Hello via myfunc.py like this :

o = myfunc.Hello()

cPickle.dumps(o)


and write it using cPickle, the module defunc_single is also pickled into the file.



So that means along the way, if you felt that you wanted a new module : lovefunc_single.py  with a better Hello and you thought that you can just replace the import statement in myfunc.py to :



from lovefunc_single import *



The cPickle will fail  to load the existing data unless you stick to defunc_single.py