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


Groups > comp.lang.python > #22579

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'instance': 0.07; 'try:': 0.07; 'python': 0.09; '"w")': 0.09; 'eof': 0.09; 'from:addr:web.de': 0.09; 'name)': 0.09; 'pickle': 0.09; 'typeerror:': 0.09; 'suggest': 0.11; "hasn't": 0.14; "objects'": 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'received:t-dialin.net': 0.16; 'subject:class': 0.16; 'test()': 0.16; 'wrote:': 0.19; 'import': 0.20; 'print': 0.21; 'keyerror:': 0.21; 'maybe': 0.22; 'implemented': 0.23; 'class': 0.25; 'classes': 0.26; 'header:User- Agent:1': 0.26; '2.6': 0.27; 'implement': 0.28; 'skip:( 20': 0.28; 'dictionary': 0.28; 'methods.': 0.28; 'header:X-Complaints-To:1': 0.29; 'skip:_ 10': 0.29; "i'm": 0.29; 'embedded': 0.30; 'objects': 0.31; "can't": 0.32; 'skip:u 20': 0.32; 'running': 0.33; 'two': 0.35; 'to:addr:python-list': 0.35; 'done': 0.35; 'subject:?': 0.35; 'received:org': 0.36; 'subject:: ': 0.37; 'except': 0.37; 'system.': 0.38; 'skip:i 20': 0.38; 'some': 0.39; 'help': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'someone': 0.40; '...': 0.40; 'received:net': 0.60; 'obvious': 0.71; 'subject:this': 0.84; 'understand,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: why can't I pickle a class containing this dispatch dictionary?
Date Tue, 03 Apr 2012 09:54:50 +0200
Organization None
References <9ee32123-6672-41f9-bdea-5ac075c71093@db5g2000vbb.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a70c.dip.t-dialin.net
User-Agent KNode/4.7.3
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.1261.1333439935.3037.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1333439935 news.xs4all.nl 6953 [2001:888:2000:d::a6]:54067
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22579

Show key headers only | View raw


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

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