Explanation about pickle module

Marco Wahl mw at visenso.de
Tue Jan 30 10:50:07 CET 2007


"raghu" <vgvr620034 at gmail.com> writes:

> can any one explain about pickle i read in the book but they have not 
> provided any example for that so please explain with a simple example
> 

>>> class Foo(object):
...   def __init__(self):
...     self.bar = 1
... 
>>> import pickle
>>> a = Foo()
>>> pickle.dumps(a)
"ccopy_reg\n_reconstructor\np0\n(c__main__\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'bar'\np6\nI1\nsb."
>>> b = pickle.loads("ccopy_reg\n_reconstructor\np0\n(c__main__\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'bar'\np6\nI1\nsb.")
>>> b.bar
1
>>> b
<__main__.Foo object at 0x402ae68c>
>>> 

There is also pickle.dumps and pickle.loads to work
directly with files.  Further there is module cpickle
which offers the same functionality as pickle but which
is faster.


Bye
-- 
Marco Wahl
http://visenso.com



More information about the Python-list mailing list