Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Ma Bell is a mean mother!


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

SubjectAuthor
o Re: How to replace an instance method?Thomas Passin

1
Re: How to replace an instance method?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: How to replace an instance method?
Date: Fri, 16 Sep 2022 19:45:42 -0400
Lines: 42
Message-ID: <mailman.451.1663372521.20444.python-list@python.org>
References: <f9f8bac0-9a27-8615-6164-38aa2d9e0fbb@t-online.de>
<CAGGBd_qKS9D6bHQ=ZwDX54e-uxWG-c4H+u6aYYAPk4oMENhgww@mail.gmail.com>
<6ea832f7-06ab-c320-8000-16ab163a592c@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de D2z8eyZ8pXs/MhqO5XEPrQkOjYUSiJCBFtZYs9xXrQ4w==
Return-Path: <list1@tompassin.net>
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=tompassin.net header.i=@tompassin.net header.b=GT2TV3A7;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
'debug': 0.07; 'maintainers': 0.07; 'dan': 0.09;
'received:23.83.212': 0.09; 'received:elm.relay.mailchannels.net':
0.09; 'but...': 0.16; 'easiest': 0.16; 'instance': 0.16;
'properly.': 0.16; 'received:10.0.0': 0.16; 'received:64.90':
0.16; 'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'static': 0.16; 'wrote:': 0.16;
'instead': 0.17; 'probably': 0.17; "can't": 0.17; '16,': 0.19;
'it?': 0.19; 'pm,': 0.19; 'to:addr:python-list': 0.20;
'basically': 0.22; 'doubt': 0.22; 'fri,': 0.22; 'maybe': 0.22;
'subject:How': 0.23; 'project.': 0.27; 'function': 0.27; 'header
:User-Agent:1': 0.30; 'code,': 0.31; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'sep': 0.32; 'but': 0.32;
"i'm": 0.33; 'someone': 0.34; 'header:In-Reply-To:1': 0.34;
'running': 0.34; 'change': 0.36; 'class': 0.37; 'way': 0.38;
'added': 0.39; 'adding': 0.39; 'text': 0.39; 'want': 0.40;
'method': 0.61; 'here': 0.62; 'ever': 0.63; 'once': 0.63;
'definition': 0.64; 'header:Received:6': 0.67; 'received:64':
0.67; 'highlight': 0.69; 'subject:method': 0.69; 'future': 0.72;
"you'll": 0.73; 'biggest': 0.84; 'inheritance': 0.84; 'method,':
0.84; 'tune': 0.84; 'fan': 0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1663371948; a=rsa-sha256;
cv=none;
b=I/9qJPF3ypsePqA+X2cNAxECJnF+mkofTy8pLl3Td7HIjpO8aqBssyIIw9x2JxAUF1CRys
BiscJY2sFM4CBbHl6/4/JRpFf2CsHlLO5BNteE17IoBpofhLSmeTZHH2tdShxCsLC59DbI
jqGQzdOmufVZxTSXkTeYgzxLPoGvj+ANK/wAEDUzQpMJ8shlA9vzT8VwkPyb/rUjMuj8j0
A+IFbPDMREBwQI1/BcosKq3LvmUGQj/Lre7iN/6UiE7mVQt0HodWp5dPMGD71KXHIBMWHW
DLQq/yzZWhJ+s3dzrk5v3/OwZRGHs1yT7PDSX0DJNSot8FSVJP8s/23g1eLt8w==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1663371948;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=RV4qA4LVUt3w6zDeA40JxToy/5YeqTwFdDbJubURFWw=;
b=G96KxMySJO5LYkotfUvPaCmbaL03qsEV535d/UgYYP382NZeI6vvchn87jnOQmxlCJ/m68
dFZt44+D3l4kqHD2gP8L2u4n6vBxG4MgEqOHHSjMukEKxKnDYzIzR+DcbISk+iKu2Fu5Yk
svZJzmJyHFfW/ntVCPfZuExVDX+BKHUtejkFoVZBHaqllCT/3CAk4kYkAnJaF4AfOngcgf
p+xLp7AShw4+IoSs5f3okzvr9+EDQ84y0TSN5uEyTEYKLsMscne8zyKAI31CB1xgErEzhB
qeWrKi0W/N5xAD7N5sArfnI0mn4mcFrUzVO+p8z0lQpe4qyBWdQ9SPMl++UxeQ==
ARC-Authentication-Results: i=1; rspamd-f776c45b8-ph9z6;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Stupid-Reign: 2e6d55a92d369ed9_1663371949105_3512661476
X-MC-Loop-Signature: 1663371949105:1267189774
X-MC-Ingress-Time: 1663371949105
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1663371948;
bh=RV4qA4LVUt3w6zDeA40JxToy/5YeqTwFdDbJubURFWw=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=GT2TV3A79tN/UXkzRiE5BZ/4PCwgmwpnxCr8L1dky44b2ZwV7rio//PwSMoQLBZIW
+eZiEjQaeZr/Hs19RY5FD9+fzKimnanQL/T2lDqQ5mJHofGJXSmowG/pTyh9S1Y08T
oGeJhGuFc2MytidMI9qOC3pMOfMy1tVt5e7BScl41cNWOlsnxkmnyIpXBLQHHA/Vuh
VFWtLCc8YGTENC6x9deCnUoUfBr01gCmXuCG05HLeaDryzgXepkzO1GrjYPVsziBVq
6zrhGhsrrfhdPuE9bE4YI8kbc5Fudd9mENdhBAmCTK0NuA8/Qcnrz4eb/7EOBvfU2r
SpwQZTR7UOMNw==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.2.2
Content-Language: en-US
In-Reply-To: <CAGGBd_qKS9D6bHQ=ZwDX54e-uxWG-c4H+u6aYYAPk4oMENhgww@mail.gmail.com>
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: <6ea832f7-06ab-c320-8000-16ab163a592c@tompassin.net>
X-Mailman-Original-References: <f9f8bac0-9a27-8615-6164-38aa2d9e0fbb@t-online.de>
<CAGGBd_qKS9D6bHQ=ZwDX54e-uxWG-c4H+u6aYYAPk4oMENhgww@mail.gmail.com>
 by: Thomas Passin - Fri, 16 Sep 2022 23:45 UTC

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 someone might want to do this kind of thing, one place is
when you are running scripts in an existing framework, and you can't
change the class definition but you can access an instance. Or you
might want to add the method to an existing class to debug and tune it
up before you go through the formalities of actually changing an
existing project.

I once added a class to an running text editor so that it would
highlight the current line. That was adding a class instead of a method,
but the need was basically the same.

On 9/16/2022 6:35 PM, Dan Stromberg wrote:
> On Fri, Sep 16, 2022 at 2:06 PM 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.
>>
>
> 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
> static analyzers will like it either.
>
> I'm not the biggest fan of inheritance you'll ever meet, but maybe this is
> a good place for it?

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor