my code(i was unable to use 'pickle'):
class A(object):
def __getstate__(self):
print 'www'
return 'sss'
def __setstate__(self,d):
print 'aaaa'
import pickle
a = A()
s = pickle.dumps(a)
e = pickle.loads(s)
print s,e
print :
www
aaaa
ccopy_reg
_reconstructor
p0
(c__main__
A
p1
c__builtin__
object
p2
Ntp3
Rp4
S'sss'
p5
b. <__main__.A object at 0x00B08CF0>
who can tell me how to use.
You are able to
pickle
(meaning, this code works as it should). You just seem to get a result, you don't expect. If you expect the same 'output', try:Your example isn't, well, a typical 'pickling' example. Usually pickled objects are saved somewhere persistently or sent over the wire. See e.g.
pickletest.py
: http://www.sthurlow.com/python/lesson10/.There are advanced uses of
pickling
, see for example David Mertz XML object serialisation article: http://www.ibm.com/developerworks/xml/library/x-matters11.htmlWhat are you trying to do? It works for me:
So this will print:
If you need setstate:
which prints:
In a nutshell, in your example, e equals a.
Don't have to care about these strang strings, you can dumps these strings to save to anywhere, just remember when you loads them, you got 'a' object again.