Lambda alternative?

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 16 16:13:18 CEST 2009


Hrvoje Niksic <hniksic at xemacs.org> wrote:

>> Correct me if i am wrong, but i can pickle an object that contains a
>> bound method (it's own bound method).
> 
> No, you can't:
> 
>>>> import cPickle as p
>>>> p.dumps([])
> '(l.'
>>>> p.dumps([].append)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: expected string or Unicode object, NoneType found

Not the best of examples: [].append is a built-in method rather than a 
bound method. You are correct that you can't pickle a bound method, but the 
actual error message is a bit different:

>>> C().foo
<bound method C.foo of <__main__.C object at 0x00B43370>>
>>> [].append
<built-in method append of list object at 0x00B3E508>
>>> cPickle.dumps(C().foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects
>>> cPickle.dumps([].append)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected string or Unicode object, NoneType found

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list