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


Groups > comp.lang.python > #22597

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dihedral88888@googlemail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'def': 0.06; 'exec': 0.07; 'instance': 0.07; 'try:': 0.07; 'python': 0.09; '"w")': 0.09; 'compression': 0.09; 'eof': 0.09; 'name)': 0.09; 'objects.': 0.09; 'pickle': 0.09; 'to:addr:comp.lang.python': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; 'suggest': 0.11; "hasn't": 0.14; '.py': 0.16; '8bit%:72': 0.16; "objects'": 0.16; 'subject:class': 0.16; 'test()': 0.16; 'wrote:': 0.19; 'import': 0.20; 'print': 0.21; 'keyerror:': 0.21; 'maybe': 0.22; 'header:In- Reply-To:1': 0.22; 'implemented': 0.23; 'class': 0.25; 'classes': 0.26; 'cc:2**0': 0.26; 'header:User-Agent:1': 0.26; '2.6': 0.27; 'operations,': 0.27; 'cc:no real name:2**0': 0.27; 'implement': 0.28; 'cc:addr:python.org': 0.28; 'skip:( 20': 0.28; 'dictionary': 0.28; 'methods.': 0.28; 'skip:_ 10': 0.29; "i'm": 0.29; 'embedded': 0.30; 'received:209.85.160': 0.30; 'objects': 0.31; "can't": 0.32; 'received:209.85': 0.32; 'skip:u 20': 0.32; 'received:google.com': 0.32; 'loading': 0.32; 'to:addr:googlegroups.com': 0.32; 'running': 0.33; 'perform': 0.33; 'from:addr:googlemail.com': 0.34; 'two': 0.35; 'done': 0.35; 'data': 0.35; 'received:209': 0.35; 'subject:?': 0.35; 'but': 0.37; 'subject:: ': 0.37; 'except': 0.37; 'system.': 0.38; 'skip:i 20': 0.38; 'some': 0.39; 'help': 0.39; 'someone': 0.40; '...': 0.40; 'increase': 0.70; 'obvious': 0.71; 'subject:this': 0.84; 'understand,': 0.84
Newsgroups comp.lang.python
Date Tue, 3 Apr 2012 07:29:31 -0700 (PDT)
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
References <9ee32123-6672-41f9-bdea-5ac075c71093@db5g2000vbb.googlegroups.com> <mailman.1261.1333439935.3037.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
MIME-Version 1.0
Subject Re: why can't I pickle a class containing this dispatch dictionary?
From 88888 Dihedral <dihedral88888@googlemail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.1274.1333463374.3037.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1333463374 news.xs4all.nl 6960 [2001:888:2000:d::a6]:35915
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22597

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 | 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