How to replace an instance method?

Eryk Sun eryksun at gmail.com
Fri Sep 16 17:34:26 EDT 2022


On 9/16/22, Ralf M. <Ralf_M at t-online.de> wrote:
> I would like to replace a method of an instance, but don't know how to
> do it properly.

A function is a descriptor that binds to any object as a method. For example:

    >>> f = lambda self, x: self + x
    >>> o = 42
    >>> m = f.__get__(o)
    >>> type(m)
    <class 'method'>
    >>> m.__self__ is o
    True
    >>> m(10)
    52


More information about the Python-list mailing list