Lost in descriptor land

eryk sun eryksun at gmail.com
Sun Jul 3 19:59:09 EDT 2016


On Sun, Jul 3, 2016 at 3:32 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>
> But if you prepare the method ahead of time, it works:
>
> from types import MethodType
> instance.method = MethodType(method, instance)

That's a fine way to bind a method, but in the context of this
"descriptor land" topic, I think it's more insightful to call the
function's __get__ method:

    >>> type(method)
    <class 'function'>
    >>> instance.method = method.__get__(instance)
    >>> type(instance.method)
    <class 'method'>
    >>> instance.method.__self__ is instance
    True
    >>> instance.method.__func__ is method
    True
    >>> instance.method()
    method called from self <__main__.Test object at 0x7faf51d1a198>



More information about the Python-list mailing list