I\'d like to pass object state between two Python programs (one is my own code running standalone, one is a Pyramid view), and different namespaces. Somewhat related questio
Solution 1
On pickle.load
, the module __main__
needs to have a function or class called MyClass
. This does not need to be the original class with the original source code. You can put other methods in it. It should work.
class MyClass(object):
pass
with open("my_c.pik", "rb") as f :
c = pickle.load(f)
Solution 2
Use the copyreg module which is used to register constructors and pickle functions to pickle specific objects. This is the example given by the module for a complex number:
def pickle_complex(c):
return complex, (c.real, c.imag)
copyreg.pickle(complex, pickle_complex, complex)
Solution 3
Override the persistent_id method of the Pickler and Unpickler. pickler.persistent_id(obj)
shall return an identifier that can be resolved by unpickler.persistent_id(id)
to the object.