Re: How to replace an instance method?

2022-09-21 Thread Diego Souza
- Diego Souza Wespa Intelligent Systems Rio de Janeiro - Brasil On Mon, Sep 19, 2022 at 1:00 PM wrote: > > > From: "Weatherby,Gerard" > Date: Mon, 19 Sep 2022 13:06:42 + > Subject: Re: How to replace an instance method? > Just subclass and override whatever method you

Re: How to replace an instance method?

2022-09-19 Thread Eryk Sun
On 9/19/22, 2qdxy4rzwzuui...@potatochowder.com <2qdxy4rzwzuui...@potatochowder.com> wrote: > On 2022-09-18 at 09:11:28 +, > Stefan Ram wrote: > >> r...@zedat.fu-berlin.de (Stefan Ram) writes (abbreviated): >> >types.MethodType( function, instance ) >> >functools.partial( function, instance )

Re: How to replace an instance method?

2022-09-19 Thread 2QdxY4RzWzUUiLuE
On 2022-09-18 at 09:11:28 +, Stefan Ram wrote: > r...@zedat.fu-berlin.de (Stefan Ram) writes (abbreviated): > >types.MethodType( function, instance ) > >functools.partial( function, instance ) > >new_method.__get__( instance ) > > I wonder which of these three possibilities expresses >

Re: How to replace an instance method?

2022-09-19 Thread Eryk Sun
On 9/18/22, Stefan Ram wrote: > r...@zedat.fu-berlin.de (Stefan Ram) writes (abbreviated): >>types.MethodType( function, instance ) >>functools.partial( function, instance ) >>new_method.__get__( instance ) > > I wonder which of these three possibilities expresses > the idea of creating a new

Re: How to replace an instance method?

2022-09-19 Thread Weatherby,Gerard
Just subclass and override whatever method you wish to modify “Private” is conceptual. Mostly it means when the next version of a module comes out, code that you wrote that accesses *._ parts of the module might break. ___ import pandas class MyClass(pandas.ExcelFile.OpenpyxlReader):

Re: How to replace an instance method?

2022-09-18 Thread Lars Liedtke
Hey, What has not been mentioned yet is simple delegation. Often you want to rewrite a method, maybe have different (more or less) parameters and additionally keep the old methods for backwards compatibility. Or mark it as deprecated at a later point. So you could write the new method and

Re: How to replace an instance method?

2022-09-17 Thread Eryk Sun
On 9/17/22, Chris Angelico wrote: > > A method IS a function. A bound method is a function with one argument > locked in, but still a function. We were talking past each other. A method object is not a function object. You're talking about a function defined in a class that's accessed as a

Re: How to replace an instance method?

2022-09-17 Thread Chris Angelico
On Sun, 18 Sept 2022 at 10:29, wrote: > > > 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

RE: How to replace an instance method?

2022-09-17 Thread avi.e.gross
ase, the function now knows what object it is attached to as this. -Original Message- From: Python-list On Behalf Of Chris Angelico Sent: Saturday, September 17, 2022 8:21 PM To: python-list@python.org Subject: Re: How to replace an instance method? On Sun, 18 Sept 2022 at 09:37, Eryk

Re: How to replace an instance method?

2022-09-17 Thread Chris Angelico
On Sun, 18 Sept 2022 at 09:37, Eryk Sun wrote: > > On 9/17/22, Chris Angelico 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

Re: How to replace an instance method?

2022-09-17 Thread Eryk Sun
On 9/17/22, Chris Angelico 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

Re: How to replace an instance method?

2022-09-17 Thread Chris Angelico
On Sun, 18 Sept 2022 at 07:20, Ralf M. wrote: > > Am 16.09.2022 um 23:34 schrieb Eryk Sun: > > On 9/16/22, Ralf M. 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

Re: How to replace an instance method?

2022-09-17 Thread Ralf M.
Am 17.09.2022 um 00:35 schrieb Dan Stromberg: On Fri, Sep 16, 2022 at 2:06 PM Ralf M. > wrote: I would like to replace a method of an instance, but don't know how to do it properly. You appear to have a good answer, but...  are you sure this is a good

Re: How to replace an instance method?

2022-09-17 Thread Ralf M.
Am 16.09.2022 um 23:34 schrieb Eryk Sun: On 9/16/22, Ralf M. 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

Re: How to replace an instance method?

2022-09-16 Thread Thomas Passin
Here is an example of probably the easiest way to add an instance method: class Demo: def sqr(self, x): return x*x # Function to turn into a instance method def cube(self, x): return x*x*x d = Demo() print(d.sqr(2)) d.cube = cube.__get__(d) print(d.cube(3)) As for when

Re: How to replace an instance method?

2022-09-16 Thread Dan Stromberg
On Fri, Sep 16, 2022 at 2:06 PM Ralf M. wrote: > I would like to replace a method of an instance, but don't know how to > do it properly. > You appear to have a good answer, but... are you sure this is a good idea? It'll probably be confusing to future maintainers of this code, and I doubt

Re: How to replace an instance method?

2022-09-16 Thread Eryk Sun
On 9/16/22, Ralf M. 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)

Re: How to replace an instance method?

2022-09-16 Thread Chris Angelico
On Sat, 17 Sept 2022 at 07:07, Ralf M. wrote: > > I would like to replace a method of an instance, but don't know how to > do it properly. > > My first naive idea was > > inst = SomeClass() > def new_method(self, param): > # do something > return whatever > inst.method = new_method > >

How to replace an instance method?

2022-09-16 Thread Ralf M.
I would like to replace a method of an instance, but don't know how to do it properly. My first naive idea was inst = SomeClass() def new_method(self, param): # do something return whatever inst.method = new_method however that doesn't work: self isn't passed as first parameter to the