Skip to content

Instantly share code, notes, and snippets.

@jjconti
Created June 8, 2018 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjconti/3705211df71c9e6e0560b80ba35f0b8f to your computer and use it in GitHub Desktop.
Save jjconti/3705211df71c9e6e0560b80ba35f0b8f to your computer and use it in GitHub Desktop.
Does pickle store methods?
>>> class A(object):
... pass
...
>>> import pickle
>>> from cStringIO import StringIO
>>> src = StringIO()
>>> p = pickle.Pickler(src)
>>> p.dump(A())
>>> datastream = src.getvalue()
>>> dst = StringIO(datastream)
>>> up = pickle.Unpickler(dst)
>>> class A(object):
... def __init__(self):
... self.a = 1
... def hi(self):
... return 'hi'
...
>>> obj = up.load()
>>> obj.hi()
'hi'
>>> obj.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'a'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment