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; 'method.': 0.05; 'instance': 0.07; 'python': 0.09; '"can\'t': 0.09; '"w")': 0.09; 'eof': 0.09; 'pickle': 0.09; 'typeerror:': 0.09; 'cc:addr:python- list': 0.10; 'mon,': 0.11; 'suggest': 0.11; 'assume': 0.12; 'sender:addr:gmail.com': 0.13; '306,': 0.16; '70,': 0.16; "objects'": 0.16; 'subject:class': 0.16; 'test()': 0.16; 'unbound': 0.16; '\xa0def': 0.16; '\xa0print': 0.16; 'skip:\xa0 30': 0.17; 'wrote:': 0.19; 'trying': 0.20; 'import': 0.20; '8bit%:3': 0.21; 'explicit': 0.21; 'keyerror:': 0.21; 'maybe': 0.22; 'header:In-Reply-To:1': 0.22; '(most': 0.23; 'references': 0.23; 'skip:" 30': 0.23; 'message-id:@mail.gmail.com': 0.24; 'class': 0.25; 'cc:2**0': 0.26; '2.6': 0.27; 'cc:no real name:2**0': 0.27; 'values': 0.28; 'cc:addr:python.org': 0.28; 'dictionary': 0.28; 'methods.': 0.28; 'traceback': 0.28; 'url:mailman': 0.29; 'skip:_ 10': 0.29; "i'm": 0.29; 'embedded': 0.30; 'objects': 0.31; "can't": 0.32; 'url:python': 0.32; 'received:209.85': 0.32; 'received:google.com': 0.32; 'running': 0.33; 'received:209.85.214': 0.33; 'method': 0.33; 'url:listinfo': 0.33; 'problem': 0.33; 'self': 0.34; 'am,': 0.34; 'two': 0.35; 'received:209': 0.35; 'subject:?': 0.35; 'url:org': 0.35; 'why': 0.37; 'michael': 0.37; 'subject:: ': 0.37; '8bit%:6': 0.38; 'system.': 0.38; 'help': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'someone': 0.40; 'recent': 0.40; '...': 0.40; '2012': 0.62; 'here': 0.64; 'obvious': 0.71; 'subject:this': 0.84; 'last):': 0.84; 'understand,': 0.84; '29,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=7pD+9eRfyFQyRFfyrGsk1e5iTg/EqiAKiIw7/qgLQzY=; b=dT1n/Fc2Jegu5a+L+A3DEopkuM4tVFS0NfX1TuT8Oy7bfI/UaQcayPB/rO5lq97vnm 2a8kk6dp1TfCkKo7GWr3IvgUk0mWcl1wuSgZ7mq5Vqb+ozJk+4WDSsW/2GuqfEbLYOrC IsUlitSLhQieSBG0Q9ovEdwGjftbj9+rcqhtN2FmW4yeGAPrNjQB8LtKpImRdV5ZZQY6 6aeh2377YdCOeqhl596JbbWxIfo4ekJ2dMp1+Rt68ME09SkgqSqIZCTXRwdPskINWbZa Xp7L+JFVhd2LeS4Z8eeQpK1eL5YZWcwEMxjBLbFu0udVR+moqY5xjC0RPoU++b0qbdVO vn1A== MIME-Version: 1.0 Sender: mhrivnak@gmail.com In-Reply-To: <9ee32123-6672-41f9-bdea-5ac075c71093@db5g2000vbb.googlegroups.com> References: <9ee32123-6672-41f9-bdea-5ac075c71093@db5g2000vbb.googlegroups.com> Date: Mon, 2 Apr 2012 18:48:45 -0400 X-Google-Sender-Auth: KYIX2WflEST8RSyS6DsRPFaEkDw Subject: Re: why can't I pickle a class containing this dispatch dictionary? From: Michael Hrivnak To: jkn Content-Type: text/plain; charset=ISO-8859-1 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: , Newsgroups: comp.lang.python Message-ID: Lines: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1333406927 news.xs4all.nl 6845 [2001:888:2000:d::a6]:35116 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22553 Pickle cannot pickle a reference to an instance method. So the problem is that self.myDict has values which are references to instance methods. Without questioning what this is trying to do or why (I assume it's a proof of concept), here is a way to make it picklable: http://pastebin.com/1zqE52mD Michael On Mon, Apr 2, 2012 at 7:17 AM, jkn wrote: > Hi All > =A0 =A0I'm clearly not understanding the 'can't pickle instancemethod > objects' error; can someone help me to understand, & maybe suggest a > workaround, (apart from the obvious if ... elif...). > > I'm running Python 2.6 on an embedded system. > > =3D=3D testpickle.py =3D=3D > import pickle > > class Test(object): > =A0 =A0def __init__(self): > =A0 =A0 =A0 =A0self.myDict =3D { > =A0 =A0 =A0 =A0 =A0 =A01: self.tag1, > =A0 =A0 =A0 =A0 =A0 =A02: self.tag2 > =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0def dispatch(self, v): > =A0 =A0 =A0 =A0try: > =A0 =A0 =A0 =A0 =A0 =A0self.myDict[v]() > =A0 =A0 =A0 =A0except KeyError: > =A0 =A0 =A0 =A0 =A0 =A0print "No corresponding dictionary entry!" > =A0 =A0 =A0 =A0# > =A0 =A0def tag1(self): > =A0 =A0 =A0 =A0print "one" > =A0 =A0def tag2(self): > =A0 =A0 =A0 =A0print "two" > > > t =3D Test() > t.dispatch(1) > t.dispatch(2) > t.dispatch(0) > > fd =3D open("pickle.out", "w") > pickle.dump(t, fd) > fd.close() > # EOF > > $ python testpickle.py > one > two > No corresponding dictionary entry! > Traceback (most recent call last): > =A0File "ptest.py", line 29, in > =A0 =A0pickle.dump(t, fd) > =A0File "/usr/lib/python2.6/pickle.py", line 1362, in dump > =A0 =A0Pickler(file, protocol).dump(obj) > =A0File "/usr/lib/python2.6/pickle.py", line 224, in dump > =A0 =A0self.save(obj) > =A0File "/usr/lib/python2.6/pickle.py", line 331, in save > =A0 =A0self.save_reduce(obj=3Dobj, *rv) > =A0File "/usr/lib/python2.6/pickle.py", line 419, in save_reduce > =A0 =A0save(state) > =A0File "/usr/lib/python2.6/pickle.py", line 286, in save > =A0 =A0f(self, obj) # Call unbound method with explicit self > =A0File "/usr/lib/python2.6/pickle.py", line 649, in save_dict > =A0 =A0self._batch_setitems(obj.iteritems()) > =A0File "/usr/lib/python2.6/pickle.py", line 663, in _batch_setitems > =A0 =A0save(v) > =A0File "/usr/lib/python2.6/pickle.py", line 286, in save > =A0 =A0f(self, obj) # Call unbound method with explicit self > =A0File "/usr/lib/python2.6/pickle.py", line 649, in save_dict > =A0 =A0self._batch_setitems(obj.iteritems()) > =A0File "/usr/lib/python2.6/pickle.py", line 663, in _batch_setitems > =A0 =A0save(v) > =A0File "/usr/lib/python2.6/pickle.py", line 306, in save > =A0 =A0rv =3D reduce(self.proto) > =A0File "/usr/lib/python2.6/copy_reg.py", line 70, in _reduce_ex > =A0 =A0raise TypeError, "can't pickle %s objects" % base.__name__ > TypeError: can't pickle instancemethod objects > $ > > > =A0 =A0Thanks > =A0 =A0J^n > > -- > http://mail.python.org/mailman/listinfo/python-list