Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Due to lack of disk space, this fortune database has been discontinued.


devel / comp.lang.python / Re: How to replace an instance method?

SubjectAuthor
o Re: How to replace an instance method?Ralf M.

1
Re: How to replace an instance method?

<mailman.453.1663449574.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19534&group=comp.lang.python#19534

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: Ral...@t-online.de (Ralf M.)
Newsgroups: comp.lang.python
Subject: Re: How to replace an instance method?
Date: Sat, 17 Sep 2022 23:11:28 +0200
Lines: 31
Message-ID: <mailman.453.1663449574.20444.python-list@python.org>
References: <f9f8bac0-9a27-8615-6164-38aa2d9e0fbb@t-online.de>
<CACL+1avBfoS0xDPE1F1CuJDSzDzi_6Ra8FT04c_-Ro1+Uk3OeQ@mail.gmail.com>
<a8db9987-7f35-6b45-1ce9-f17a880dc00a@t-online.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de UMz/byyLrmrmGusAikTNqAJDtAx81rgzDxeVcU9c1A4Q==
Return-Path: <Ralf_M@t-online.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.016
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'library.': 0.05;
'example.': 0.09; 'example:': 0.09; 'schrieb': 0.09; 'import':
0.15; 'eryk': 0.16; 'lambda': 0.16; 'properly.': 0.16; 'toy':
0.16; 'wrote:': 0.16; 'probably': 0.17; 'to:addr:python-list':
0.20; 'code': 0.23; 'subject:How': 0.23; 'received:de': 0.23;
'tried': 0.26; 'object': 0.26; 'function': 0.27; '>>>': 0.28;
'chris': 0.28; 'suggestions': 0.28; 'header:User-Agent:1': 0.30;
'but': 0.32; 'header:In-Reply-To:1': 0.34; 'received:192.168':
0.37; 'though': 0.37; 'two': 0.39; 'least': 0.39; 'use': 0.39;
'both': 0.40; 'try': 0.40; 'method': 0.61; 'true': 0.63;
'subject:method': 0.69; 'received:194': 0.70; 'skip:f 20': 0.75;
'extra': 0.84
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.2.2
In-Reply-To: <CACL+1avBfoS0xDPE1F1CuJDSzDzi_6Ra8FT04c_-Ro1+Uk3OeQ@mail.gmail.com>
X-TOI-EXPURGATEID: 150726::1663449091-49FF7689-69F80B7A/0/0 CLEAN NORMAL
X-TOI-MSGID: 25150858-5d02-46a5-aa75-2bdd124e19ad
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <a8db9987-7f35-6b45-1ce9-f17a880dc00a@t-online.de>
X-Mailman-Original-References: <f9f8bac0-9a27-8615-6164-38aa2d9e0fbb@t-online.de>
<CACL+1avBfoS0xDPE1F1CuJDSzDzi_6Ra8FT04c_-Ro1+Uk3OeQ@mail.gmail.com>
 by: Ralf M. - Sat, 17 Sep 2022 21:11 UTC

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

Thank you and Chris A. for the two suggestions how to replace a method.

I tried both
inst.method = functools.partial(new_method, inst)
and
inst.method = new_method.__get__(inst)
and both work in my toy example.
I will try it on the real code next week.

Even though the functools.partial solution is easier to understand (at
least for me), I will probably use the __get__ solution as it avoids
the import of an extra library.

Ralf M.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor