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


Groups > comp.lang.python > #22601

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!newsfeed5.news.xs4all.nl!xs4all!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'def': 0.06; 'class,': 0.07; 'instance': 0.07; 'from:addr:web.de': 0.09; 'name):': 0.09; 'pickle': 0.09; '>>>': 0.14; "hasn't": 0.14; 'a(object):': 0.16; 'functools': 0.16; 'new-style': 0.16; "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; 'type:': 0.16; 'wrote:': 0.19; 'import': 0.20; 'written': 0.21; 'print': 0.21; 'please?': 0.21; 'implemented': 0.23; 'class': 0.25; "doesn't": 0.25; 'classes': 0.26; 'header:User-Agent:1': 0.26; 'protocol': 0.27; 'methods.': 0.28; 'hi,': 0.29; 'header:X-Complaints-To:1': 0.29; "skip:' 10": 0.29; 'skip:_ 10': 0.29; "i'm": 0.29; 'url:python': 0.32; 'am,': 0.34; 'mean': 0.34; 'to:addr:python-list': 0.35; 'done': 0.35; 'subject:?': 0.35; 'url:org': 0.35; 'received:org': 0.36; 'similar': 0.36; 'object': 0.37; 'subject:: ': 0.37; 'some': 0.39; 'help': 0.39; 'hello,': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'someone': 0.40; 'received:net': 0.60; 'url:c': 0.75; 'subject:this': 0.84; 'otten': 0.84; 'understand,': 0.84; 'url:cpython': 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 16:44:09 +0200
Organization None
References <9ee32123-6672-41f9-bdea-5ac075c71093@db5g2000vbb.googlegroups.com> <mailman.1261.1333439935.3037.python-list@python.org> <4db7aeea-c23c-4e43-b67c-4499409ec5f5@i18g2000vbx.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.1276.1333464262.3037.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1333464262 news.xs4all.nl 6924 [2001:888:2000:d::a6]:47557
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22601

Show key headers only | View raw


jkn wrote:

> Hi Peter
> 
> On Apr 3, 8:54 am, Peter Otten <__pete...@web.de> wrote:
>> 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.
> 
> by 'classes implemented in C', doyou mean new-style classes', or what,
> please?

Given 

>>> class A(object):
...     def __init__(self, name):
...             self.name = name
...     def hello(self):
...             print "Hello,", self.name
... 
>>> a = A("Peter")
>>> hello = a.hello
>>> hello()
Hello, Peter

the object bound to the name 'hello' is an instance of the 'instancemethod' 
type:

>>> type(hello)
<type 'instancemethod'>

That type is implemented in C, see

http://hg.python.org/cpython/file/9599f091faa6/Objects/classobject.c

and doesn't support the pickle protocol while a similar class, 
functools.partial which is also written in C, see

http://hg.python.org/cpython/file/9599f091faa6/Modules/_functoolsmodule.c

does:

>>> from functools import partial
>>> import pickle
>>> def hello(obj):
...     print "Hi,", obj.name
... 
>>> hello2 = partial(hello, a)
>>> hello2()
Hi, Peter
>>> s = pickle.dumps(hello2)
>>> pickle.loads(s)()
Hi, Peter

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