[Tutor] cannot pickle instancemethod objects

Andreas Kostyrka andreas at kostyrka.org
Tue Jun 12 12:51:36 CEST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pickle is not capable of pickling class objects. Only instances of the
class.

Actually, pickle only records the name of the class an object belongs
to, so renaming or moving pickled classes means additional work, to map
old to new names.

Andreas

hok kakada wrote:
> Dear everyone,
> 
> I got a problem while pickling a class object. I have done something as below:
>         pickleFile = open(filename, 'wb')
>         pickle.dump(matcher, pickleFile)
>         pickleFile.close()
> 
> where matcher is  class object and I got the follow errors:
> Traceback (most recent call last):
>   File "/usr/lib/python2.5/pickle.py", line 1362, in dump
>     Pickler(file, protocol).dump(obj)
>   File "/usr/lib/python2.5/pickle.py", line 224, in dump
>     self.save(obj)
>   File "/usr/lib/python2.5/pickle.py", line 286, in save
>     f(self, obj) # Call unbound method with explicit self
>   File "/usr/lib/python2.5/pickle.py", line 725, in save_inst
>     save(stuff)
>   File "/usr/lib/python2.5/pickle.py", line 286, in save
>     f(self, obj) # Call unbound method with explicit self
>   File "/usr/lib/python2.5/pickle.py", line 649, in save_dict
>     self._batch_setitems(obj.iteritems())
>   File "/usr/lib/python2.5/pickle.py", line 663, in _batch_setitems
>     save(v)
>   File "/usr/lib/python2.5/pickle.py", line 286, in save
>     f(self, obj) # Call unbound method with explicit self
>   File "/usr/lib/python2.5/pickle.py", line 725, in save_inst
>     save(stuff)
>   File "/usr/lib/python2.5/pickle.py", line 286, in save
>     f(self, obj) # Call unbound method with explicit self
>   File "/usr/lib/python2.5/pickle.py", line 649, in save_dict
>     self._batch_setitems(obj.iteritems())
>   File "/usr/lib/python2.5/pickle.py", line 663, in _batch_setitems
>     save(v)
>   File "/usr/lib/python2.5/pickle.py", line 306, in save
>     rv = reduce(self.proto)
>   File "/usr/lib/python2.5/copy_reg.py", line 69, in _reduce_ex
>     raise TypeError, "can't pickle %s objects" % base.__name__
> TypeError: can't pickle instancemethod objects
> 
> Does anybody please point me what could be a problem here?
> 
> Thanks very much for your help!
> da
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGbnq4HJdudm4KnO0RAstPAKCTAi8vAGak79KDNtJGqZFq27Hr7ACfeIFq
w5kcsggZZCnJDJWWk1bTOb8=
=aWNx
-----END PGP SIGNATURE-----


More information about the Tutor mailing list