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?

From Peter Otten <__peter__@web.de>
Subject Re: why can't I pickle a class containing this dispatch dictionary?
Date 2012-04-03 16:44 +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>
Newsgroups comp.lang.python
Message-ID <mailman.1276.1333464262.3037.python-list@python.org> (permalink)

Show all headers | 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