How to replace an instance method?
avi.e.gross at gmail.com
avi.e.gross at gmail.com
Sat Sep 17 20:28:05 EDT 2022
>From your description, Chris, it sounds like the functional programming
technique often called currying. A factory function is created where one (or
more) parameters are sort of frozen in so the user never sees or cares about
them, and a modified or wrapped function is returned. In this case, the
function now knows what object it is attached to as this.
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Chris Angelico
Sent: Saturday, September 17, 2022 8:21 PM
To: python-list at python.org
Subject: Re: How to replace an instance method?
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
--
https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list