[Python-porting] Pickling unbound methods on Python 3

cool-RR cool-rr at cool-rr.com
Sat May 29 22:52:11 CEST 2010


Hello Martin, thanks for joining the discussion.

On Sat, May 29, 2010 at 8:43 PM, "Martin v. Löwis" <martin at v.loewis.de>wrote:

> I know that unbound methods are of the function type in Python 3. I'm
>> calling them unbound methods because they are methods of classes which
>> are not bound to an instance. I'm aware they have no special stance, but
>> I still refer to them as unbound methods.
>>
>
> Neglecting reality doesn't help your cause, though. You'll need to
> understand *why* pickling fails, and then see whether there might be
> a solution.
>

I think I understand why pickling fails. An unbound method is
not differentiated in any way from a function. The `save_global` function
tries to look for it in the main module namespace, instead of in the class.



> A work-around is to put all methods you want to pickle into module-scope
> of your module
>
> class Foo:
>  def bar(self):
>    pass
>
> bar = Foo.bar
>
> Now you can pickle Foo.bar.
>

Yes, I've already done this, but I'd prefer a less hackish solution.



> Regards,
> Martin
>

I can think of a few solutions:

1. Make `save_global` look in the classes inside the module as well.

2. Allow me to specify a reducer for FunctionType. This is currently not
working, see here:
http://stackoverflow.com/questions/2932742/python-using-copyreg-to-define-reducers-for-types-that-already-have-reducers


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20100529/faf0bbbd/attachment.html>


More information about the Python-porting mailing list