replacement for new.instancemethod in Python3?

S

skip

Is there a replacement in Python3 for new.instancemethod? That is, given an
arbitrary instance (not its class) how can I add a new appropriately defined
function as a method to it?

Thx,
 
M

Michele Simionato

Is there a replacement in Python3 for new.instancemethod?  That is, given an
arbitrary instance (not its class) how can I add a new appropriately defined
function as a method to it?

There is no need for new.instancemethod for new style classes:
<bound method C.f of <__main__.C object at 0xb7b6fdec>>
 
S

skip

Michele> There is no need for new.instancemethod for new style classes:
<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
 
M

Michele Simionato

    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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top