[Python-Dev] Unbinding of methods

M Stefan mstefanro at gmail.com
Fri Jul 20 00:32:58 CEST 2012

On 7/19/2012 9:54 PM, Antoine Pitrou wrote:
> On Thu, 19 Jul 2012 19:53:27 +0300
> M Stefan <mstefanro at gmail.com> wrote:
>> Hey,
>> As part of pickle4, I found it interesting to add the possibility
>> of pickling bound functions (instance methods). This is done by
>> pickling f.__self__ and f.__func__ separately, and then adding
>> a BIND opcode to tie them together.
> Instead of a specific opcode, can't you use a suitable __reduce__ magic
> (or __getnewargs__, perhaps)? We want to limit the number of opcodes
> except for performance-critical types (and I don't think bound methods
> are performance-critical for the purpose of serialization).
Yes, I agree that doing it with __reduce__ would be better approach than
adding a new opcode, I'll consider switching.
>> I have created a patch at [1], which adds __func__ to some other
>> function types, as well as:
>>    1) adds AllFunctionTypes etc. to Lib/types.py
>>    2) inspect.isanyfunction(), inspect.isanyboundfunction(),
>>       inspect.isanyunboundfunction()
>>    3) functools.unbind
> That sounds like a lot of changes if the goal is simply to make those
> types picklable.
> Regards
> Antoine.
Indeed they are, I just thought there may be a chance this code would be 
used elsewhere
too. It's a bit weird that you can use inspect to check for certain 
types of functions but
not others, as well as be able to "unbind" certain types of methods but 
not others.
Admittedly, these changes have little use-case and are not a priority.

  Stefan M

More information about the Python-Dev mailing list