Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22553
| 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 | <mhrivnak@gmail.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; '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 <mhrivnak@hrivnak.org> |
| To | jkn <jkn_gg@nicorp.f9.co.uk> |
| 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 <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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1244.1333406927.3037.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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 <jkn_gg@nicorp.f9.co.uk> wrote:
> Hi All
> I'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.
>
> == 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!
> Traceback (most recent call last):
> File "ptest.py", line 29, in <module>
> pickle.dump(t, fd)
> File "/usr/lib/python2.6/pickle.py", line 1362, in dump
> Pickler(file, protocol).dump(obj)
> File "/usr/lib/python2.6/pickle.py", line 224, in dump
> self.save(obj)
> File "/usr/lib/python2.6/pickle.py", line 331, in save
> self.save_reduce(obj=obj, *rv)
> File "/usr/lib/python2.6/pickle.py", line 419, in save_reduce
> save(state)
> File "/usr/lib/python2.6/pickle.py", line 286, in save
> f(self, obj) # Call unbound method with explicit self
> File "/usr/lib/python2.6/pickle.py", line 649, in save_dict
> self._batch_setitems(obj.iteritems())
> File "/usr/lib/python2.6/pickle.py", line 663, in _batch_setitems
> save(v)
> File "/usr/lib/python2.6/pickle.py", line 286, in save
> f(self, obj) # Call unbound method with explicit self
> File "/usr/lib/python2.6/pickle.py", line 649, in save_dict
> self._batch_setitems(obj.iteritems())
> File "/usr/lib/python2.6/pickle.py", line 663, in _batch_setitems
> save(v)
> File "/usr/lib/python2.6/pickle.py", line 306, in save
> rv = reduce(self.proto)
> File "/usr/lib/python2.6/copy_reg.py", line 70, in _reduce_ex
> raise TypeError, "can't pickle %s objects" % base.__name__
> TypeError: can't pickle instancemethod objects
> $
>
>
> Thanks
> J^n
>
> --
> http://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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