replacement for new.instancemethod in Python3?

skip at pobox.com skip at pobox.com
Sun Apr 5 14:07:26 CEST 2009


> Is there a replacement in Python3 for new.instancemethod?

I think I can answer my own question: functools.partial:

    >>> class C:
    ...   pass
    ... 
    >>> def meth(self, x):
    ...   self.x = x
    ... 
    >>> c = C()
    >>> c.meth = meth
    >>> c.meth(5)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: meth() takes exactly 2 positional arguments (1 given)
    >>> import functools
    >>> c.meth = functools.partial(meth, c)
    >>> c.meth(5)
    >>> c.x
    5

Is this the "one obvious" way to do this in Python3?

-- 
Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/
        "XML sucks, dictionaries rock" - Dave Beazley



More information about the Python-list mailing list