Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22596

Re: why can't I pickle a class containing this dispatch dictionary?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.python
Subject Re: why can't I pickle a class containing this dispatch dictionary?
Date Tue, 3 Apr 2012 07:29:31 -0700 (PDT)
Organization http://groups.google.com
Lines 76
Message-ID <6406461.2791.1333463371767.JavaMail.geo-discussion-forums@pbnt10> (permalink)
References <9ee32123-6672-41f9-bdea-5ac075c71093@db5g2000vbb.googlegroups.com> <mailman.1261.1333439935.3037.python-list@python.org>
NNTP-Posting-Host 123.204.72.177
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1333463372 31141 127.0.0.1 (3 Apr 2012 14:29:32 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Tue, 3 Apr 2012 14:29:32 +0000 (UTC)
Cc python-list@python.org
In-Reply-To <mailman.1261.1333439935.3037.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=123.204.72.177; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw
User-Agent G2/1.0
Xref csiph.com comp.lang.python:22596

Show key headers only | View raw


Peter Otten於 2012年4月3日星期二UTC+8下午3時54分50秒寫道:
> jkn wrote:
> 
> >     I'm clearly not understanding the 'can't pickle instancemethod
> > objects' error; can someone help me to understand, 
> 
> I think classes implemented in C need some extra work to make them 
> picklable, and that hasn't been done for instance methods.
> 
> > & maybe suggest a
> > workaround, (apart from the obvious if ... elif...).
> 
> You can implement pickling yourself:
> 
> import copy_reg 
> import types
> 
> def pickle_instancemethod(m):
>     return unpickle_instancemethod, (m.im_func.__name__, m.im_self, 
> m.im_class) 
>  
> def unpickle_instancemethod(name, im_self, im_class):
>     im_func = getattr(im_class, name)
>     return im_func.__get__(im_self, im_class) 
>  
> copy_reg.pickle(types.MethodType, pickle_instancemethod) 
> 
>  
> > I'm running Python 2.6 on an embedded system.
> > 
> > == testpickle.py ==
> > import pickle
> > 
> > class Test(object):
> >     def __init__(self):
> >         self.myDict = {
> >             1: self.tag1,
> >             2: self.tag2
> >             }
> >     def dispatch(self, v):
> >         try:
> >             self.myDict[v]()
> >         except KeyError:
> >             print "No corresponding dictionary entry!"
> >         #
> >     def tag1(self):
> >         print "one"
> >     def tag2(self):
> >         print "two"
> > 
> > 
> > t = Test()
> > t.dispatch(1)
> > t.dispatch(2)
> > t.dispatch(0)
> > 
> > fd = open("pickle.out", "w")
> > pickle.dump(t, fd)
> > fd.close()
> > # EOF
> > 
> > $ python testpickle.py
> > one
> > two
> > No corresponding dictionary entry!
> 
> > TypeError: can't pickle instancemethod objects
> > $

Save your python files as a package in .pyd or .py  and use exec to get what you want. Of course you can use the data compression package to perform 
serialization operations, but that will increase start up time in loading your objects.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

why can't I pickle a class containing this dispatch dictionary? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-04-02 04:17 -0700
  Re: why can't I pickle a class containing this dispatch dictionary? Michael Hrivnak <mhrivnak@hrivnak.org> - 2012-04-02 18:48 -0400
  Re: why can't I pickle a class containing this dispatch dictionary? Peter Otten <__peter__@web.de> - 2012-04-03 09:54 +0200
    Re: why can't I pickle a class containing this dispatch dictionary? jkn <jkn_gg@nicorp.f9.co.uk> - 2012-04-03 05:57 -0700
      Re: why can't I pickle a class containing this dispatch dictionary? Peter Otten <__peter__@web.de> - 2012-04-03 16:44 +0200
    Re: why can't I pickle a class containing this dispatch dictionary? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-04-03 07:29 -0700
    Re: why can't I pickle a class containing this dispatch dictionary? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-04-03 07:29 -0700

csiph-web