Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

New crypt. See /usr/news/crypt.


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

SubjectAuthor
o Re: How to replace an instance method?Chris Angelico

1
Re: How to replace an instance method?

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

  copy mid

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

  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: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: How to replace an instance method?
Date: Sun, 18 Sep 2022 07:27:19 +1000
Lines: 40
Message-ID: <mailman.455.1663450053.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>
<CAPTjJmoPhVkGRqLzCUFDntp2JpR5VX_1qTYJJtj8AnAtDOAWnA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de mIUvyWQccJXo0mlB+gGmcwdHEuMPWfl66E8cqLt3NLrQ==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=qtkCKACK;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '2022': 0.05; 'library.':
0.05; 'sun,': 0.07; 'example.': 0.09; 'example:': 0.09; 'schrieb':
0.09; 'import': 0.15; 'chrisa': 0.16; 'eryk': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'lambda': 0.16; 'properly.': 0.16; 'received:209.85.218': 0.16;
'sept': 0.16; 'toy': 0.16; 'whichever': 0.16; 'wrote:': 0.16;
'probably': 0.17; 'to:addr:python-list': 0.20; 'basically': 0.22;
'code': 0.23; 'subject:How': 0.23; 'tried': 0.26; 'object': 0.26;
'function': 0.27; '>>>': 0.28; 'chris': 0.28; 'fact': 0.28;
'sense': 0.28; 'suggestions': 0.28; 'message-id:@mail.gmail.com':
0.32; 'but': 0.32; 'same': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'functions': 0.36; 'using': 0.37; 'received:209.85': 0.37;
'though': 0.37; 'received:209': 0.39; 'two': 0.39; 'least': 0.39;
'use': 0.39; 'both': 0.40; 'try': 0.40; 'method': 0.61; 'true':
0.63; 'choose': 0.67; 'subject:method': 0.69; 'skip:f 20': 0.75;
'extra': 0.84; 'emphasizes': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date;
bh=NmdWsqU4P6Rzp9yxT2PcPQ8QDAj/yA6GlasAeeR82Y8=;
b=qtkCKACKah8xn9/GHz64Jxxc6nkErh2uIu+hd/DkLpfbSO3E7A9N/z6s0r9WqUbnu5
ALVHYAcWQf78WUD6xcl4/5JvOTlECTcjgnnECKIGU7+weLPIqwBxF380wx095xzysJUS
Zi/OTsKEKYoW7L59r1HHRff4O79QeuDrwc4h/6czPi5zM+fuJCXS496AEZexmmGTihSL
Rug/ZdWlZgcLP16qduKobQtXIfg6jprV12/jp4HUkjid6cDuLh0RfeEgwC0cFwDLfkZf
rN9z6kqlrDos7qLJbSVIc480VxwC4+1V6s7R6n2bEcuLxbAZBRJBlG4v53VMXk8s1vIk
rGyg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date;
bh=NmdWsqU4P6Rzp9yxT2PcPQ8QDAj/yA6GlasAeeR82Y8=;
b=EGKeJYsRngB3j/6C0vSB0wKfN6qQpR3hSMHWLqt8cV+frYp0cfdabZ/xOPgWqEVlkG
OHqPS4hO5oveCuD7VkcEY7I4ihsAOYU4eDQWGIrxw8xrcaee31BgQmbxnUVhxRp89OWP
kOY1N8MRopLBckEOdStrGCnMhgQcfXAIbvZQ59TbNlmJzDPOwvRfavQggm5stPLofpd2
b324FLl5+Y+osytLSs16t4OWK6j2pZ9DeixpMtlCQldNT34DThlSVZUUp7xgGR4woJ6o
NtuM8YBxRHnRen+cIdRn+eC0niuU3N0KYkwZ76CI6jiPJUarqgZwDHADTwNSqMpMGSzh
jAQg==
X-Gm-Message-State: ACrzQf0/2HViS3tm5TXVd9TZ1KyxgBwrhZnLDr9t2QiC2n9PlZegssrf
1i/u/uQw5UKHxpqZn4XIyxv0kdkCgIe6GfHb2Ku3Jaf0
X-Google-Smtp-Source: AMsMyM6wFCrJU0FKNpyk8NSJsCrVkqxooPUsfyegNt+T+l1JzV1l149rOAv1H9N8TGKg6r8YW1SOwlRSXTTw9IS2/Iw=
X-Received: by 2002:a17:906:58ce:b0:781:b05:772f with SMTP id
e14-20020a17090658ce00b007810b05772fmr1492932ejs.581.1663450050871; Sat, 17
Sep 2022 14:27:30 -0700 (PDT)
In-Reply-To: <a8db9987-7f35-6b45-1ce9-f17a880dc00a@t-online.de>
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: <CAPTjJmoPhVkGRqLzCUFDntp2JpR5VX_1qTYJJtj8AnAtDOAWnA@mail.gmail.com>
X-Mailman-Original-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>
 by: Chris Angelico - Sat, 17 Sep 2022 21:27 UTC

On Sun, 18 Sept 2022 at 07:20, Ralf M. <Ralf_M@t-online.de> wrote:
>
> 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.
>

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!

ChrisA


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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor