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: 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: 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> 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: 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 Peter Otten=E6=96=BC 2012=E5=B9=B44=E6=9C=883=E6=97=A5=E6=98=9F=E6=9C=9F=E4= =BA=8CUTC+8=E4=B8=8B=E5=8D=883=E6=99=8254=E5=88=8650=E7=A7=92=E5=AF=AB=E9= =81=93=EF=BC=9A > jkn wrote: >=20 > > I'm clearly not understanding the 'can't pickle instancemethod > > objects' error; can someone help me to understand,=20 >=20 > I think classes implemented in C need some extra work to make them=20 > picklable, and that hasn't been done for instance methods. >=20 > > & maybe suggest a > > workaround, (apart from the obvious if ... elif...). >=20 > You can implement pickling yourself: >=20 > import copy_reg=20 > import types >=20 > def pickle_instancemethod(m): > return unpickle_instancemethod, (m.im_func.__name__, m.im_self,=20 > m.im_class)=20 > =20 > def unpickle_instancemethod(name, im_self, im_class): > im_func =3D getattr(im_class, name) > return im_func.__get__(im_self, im_class)=20 > =20 > copy_reg.pickle(types.MethodType, pickle_instancemethod)=20 >=20 > =20 > > I'm running Python 2.6 on an embedded system. > >=20 > > =3D=3D testpickle.py =3D=3D > > import pickle > >=20 > > class Test(object): > > def __init__(self): > > self.myDict =3D { > > 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" > >=20 > >=20 > > t =3D Test() > > t.dispatch(1) > > t.dispatch(2) > > t.dispatch(0) > >=20 > > fd =3D open("pickle.out", "w") > > pickle.dump(t, fd) > > fd.close() > > # EOF > >=20 > > $ python testpickle.py > > one > > two > > No corresponding dictionary entry! >=20 > > TypeError: can't pickle instancemethod objects > > $ Save your python files as a package in .pyd or .py and use exec to get wha= t you want. Of course you can use the data compression package to perform= =20 serialization operations, but that will increase start up time in loading y= our objects.