replacement for new.instancemethod in Python3?

Michele Simionato michele.simionato at gmail.com
Sun Apr 5 10:18:01 EDT 2009


On Apr 5, 3:58 pm, s... at pobox.com wrote:
>     Michele> There is no need for new.instancemethod for new style classes:
>
>     >>> class C: pass
>     ...
>     >>> c=C()
>     >>> def f(self): pass
>     ...
>     >>> c.f = f.__get__(c, C)
>     >>> c.f
>     <bound method C.f of <__main__.C object at 0xb7b6fdec>>
>
> Like a chimpanzee I can mimic your use of __get__ (that is, use the pattern
> you've defined without understanding what it means), but based on the 3.1
> docs I haven't the slightest idea what it's really supposed to do:
>
>     object.__get__(self, instance, owner)
>         Called to get the attribute of the owner class (class attribute
>         access) or of an instance of that class (instance attribute
>         access). owner is always the owner class, while instance is the
>         instance that the attribute was accessed through, or None when the
>         attribute is accessed through the owner. This method should return
>         the (computed) attribute value or raise an AttributeError exception.
>
> And what does object.__set__ do?
>
>     object.__set__(self, instance, value)
>         Called to set the attribute on an instance instance of the owner
>         class to a new value, value.
>
> What attribute on the instance?
>
> I rather like functools.partial better than object.__get__.  At least I can
> understand it.
>
> Skip

Perhaps the docstring should contain a link to Raymond
Hettinger's descriptor essay: http://users.rcn.com/python/download/Descriptor.htm



More information about the Python-list mailing list