replacement for new.instancemethod in Python3?

skip at pobox.com skip at pobox.com
Sun Apr 5 15:58:16 CEST 2009


    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



More information about the Python-list mailing list