How to replace an instance method?

Chris Angelico rosuav at gmail.com
Sat Sep 17 20:21:07 EDT 2022


On Sun, 18 Sept 2022 at 09:37, Eryk Sun <eryksun at gmail.com> wrote:
>
> On 9/17/22, Chris Angelico <rosuav at gmail.com> wrote:
> >
> > The two are basically equivalent. Using functools.partial emphasizes
> > the fact that all you're doing is "locking in" the first parameter;
> > using the __get__ method emphasizes the fact that functions are,
> > fundamentally, the same thing as methods. Choose whichever one makes
> > sense to you!
>
> Functions are really not "fundamentally, the same thing as methods".
> They're only the same in that they're both callable. Also, a method's
> __getattribute__() falls back on looking up attributes on the
> underlying function (i.e. the method's __func__), such as inspecting
> the __name__ and __code__. A fundamental difference is that, unlike a
> function, a method is not a descriptor. Thus if a method object is set
> as an attribute of a type, the method does not rebind as a new method
> when accessed as an attribute of an instance of the type.

An unbound method in Python 2 was distinctly different from a
function, but in Python 3, they really truly are the same thing. A
bound method object is a small wrapper around a function which binds
its 'self' parameter; that's a distinction, but not a fundamental one.
Yes, a bound method isn't a descriptor; that's not really a huge
difference either, though.

A method IS a function. A bound method is a function with one argument
locked in, but still a function.

ChrisA


More information about the Python-list mailing list