I would like to store a class and many instances for later use, or to give to someone else.
So far I can pickle and recover the instances, but I have to recreate the class by hand before loading them.
I've looked at this documentation which leads me to believe I should be able to do this somehow, but I can't seem to find out exactly how to do it.
EDIT: I've read this answer discussing the use of dill
(see this answer also), but I don't have dill
installed. I'd like a pickle solution if it exists.
import numpy as np
import pickle
class wow(object):
def __init__(self, x):
self.x = x
w5 = wow(np.arange(5))
w3 = wow(range(3))
with open("w5w3.pickle", "w") as outfile:
pickle.dump([w5, w3], outfile)
# save the class also
with open("wow.pickle", "w") as outfile:
pickle.dump(wow, outfile)
# OK, now delete class wow, then try to recover the pickles
del wow, w3, w5
try:
with open("wow.pickle", "r") as infile:
wow = pickle.load(infile)
except Exception, e: # returns: "'module' object has no attribute 'wow'"
print str(e)
print "so manually recreate class wow"
class wow(object):
def __init__(self, x):
self.x = x
with open("w5w3.pickle", "r") as infile:
W = pickle.load(infile)
for thing in W:
print type(thing.x), thing.x
you must open pickle file in binary mode as it mentioned here:
If you are concerned about security, both
dill
andpickle
have the same issues. However, if you want ease of use,dill
handles the exact case you are looking to handle. You can dynamically create a class and then save it's instance withdill
… then pass the instance to an environment that doesn't have the class definition, anddill
will still be able to recreate the class and the class instance. This is because, unlikepickle
,dill
stores the class definition in the pickle (by default) as well as the class instance's state.Then ship the file to another computer… restart a session, and like magic, it works.
In short, I disagree with all of the other answers. You've even linked an answer in your question that shows that class instances, class instance state, and class code can be pickled. Pickle python class instance plus definition
Generally you can pickle any object if you can pickle every attribute of that object. Classes, functions, and methods cannot be pickled.
Source: https://wiki.python.org/moin/UsingPickle
Don't destroy the class or import it as a module.
I believe the error is caused because you deleted the class definition. Object serialization in Python (which to my knowledge is also in Java) requires the class definition to be there.
From your linked documentation:
If you want to send your friend the class and instances, send the class through a code defining the class
wow
, and the instances through the pickle file.