Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

For large values of one, one equals two, for small values of two.


devel / comp.lang.python / Re: How does a method of a subclass become a method of the base class?

SubjectAuthor
o Re: How does a method of a subclass become a method of the base class?Jen Kris

1
Re: How does a method of a subclass become a method of the base class?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!2.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: jenk...@tutanota.com (Jen Kris)
Newsgroups: comp.lang.python
Subject: Re: How does a method of a subclass become a method of the base class?
Date: Sun, 26 Mar 2023 21:24:01 +0200 (CEST)
Lines: 103
Message-ID: <mailman.2403.1679858643.20444.python-list@python.org>
References: <NRU0CaG--3-9@tutanota.com>
<db65ddf5-4946-a84f-e064-41e6e37b874d@Damon-Family.org>
<NRUN9Tc--3-9@tutanota.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de JjKMeVbj1EymVTzRQOBA+gpZ8K/xc3KRFKicVlZmHEwg==
Return-Path: <jenkris@tutanota.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=tutanota.com header.i=@tutanota.com header.b=tNSyBmFP;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'class,':
0.05; 'indicate': 0.05; '26,': 0.07; 'class.': 0.07; 'mar': 0.07;
'skip:\xc2 30': 0.07; 'cc:addr:python-list': 0.09; 'damon': 0.09;
'derived': 0.09; 'else:': 0.09; 'methods,': 0.09; 'skip:\xc2 20':
0.09; 'subject:class': 0.09; 'support,': 0.09; 'cc:no real
name:2**0': 0.14; 'url:mailman': 0.15; 'built,': 0.16;
'connection.': 0.16; 'constraint,': 0.16; 'followup': 0.16;
'found,': 0.16; 'holzer': 0.16; 'level,': 0.16; 'object,': 0.16;
'subject:does': 0.16; 'use?': 0.16; 'wrote:': 0.16; 'instead':
0.17; 'uses': 0.19; 'pm,': 0.19; 'cc:addr:python.org': 0.20;
'skip:_ 10': 0.22; 'subject:How': 0.23; 'received:de': 0.23; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-ip:188.166/16': 0.25;
'behavior': 0.26; 'object': 0.26; 'creating': 0.27; 'fact': 0.28;
'approach': 0.31; 'url-ip:188/8': 0.31; 'program': 0.31;
"doesn't": 0.32; 'question': 0.32; 'concept': 0.32; 'python-list':
0.32; 'mark': 0.32; 'received:192.168.1': 0.32; 'but': 0.32;
"i'm": 0.33; 'there': 0.33; 'use,': 0.33; 'mean': 0.34; 'header
:In-Reply-To:1': 0.34; 'final': 0.35; 'also,': 0.36; 'thanks,':
0.36; 'those': 0.36; 'class': 0.37; 'received:192.168': 0.37;
'put': 0.38; 'thanks': 0.38; 'use': 0.39; 'methods': 0.39; 'skip:u
20': 0.39; 'base': 0.40; 'define': 0.40; 'method': 0.61;
'remember': 0.61; 'skip:\xc2 10': 0.62; 'true': 0.63; 'skip:b 10':
0.63; 'key': 0.64; 'richard': 0.64; 'your': 0.64; 'similar': 0.65;
'strength': 0.69; 'subject:method': 0.69; '8bit%:6': 0.71;
'global': 0.73; '8bit%:100': 0.76; 'chain': 0.76; 'method,': 0.84;
'subject: \n ': 0.84; 'to:addr:richard': 0.84; 'duplicate': 0.93
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1679858641;
s=s1; d=tutanota.com;
h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender;
bh=sC0pqcpSC2MuR4W5i2tZqB/rgBagP6FJck88FDm6LzE=;
b=tNSyBmFP70UMDZG+gRigIu7EhsSusGAxXf07bwZqAElKZfMfbWYVaNxiXj5oYrFs
gEN1T4s4hr9eIC5/TZC9DXkjF5H943Tq9FSYypyNDAKcKQ/s8+L3vx43iKYbIFKCOcL
DhYSA2YRyFr5pvKHspKz9jUcngoQYrMh27mWCVMhxanluUIweYncEVCvfGIdmhttAEX
iBlULoNrkJg2jY7DR8N2AeiAb8uCvJ4mfOXKzbYhCNU1FY7Y1FFnRQMT0hlvZd5is9F
L9ZiCxA1ivh55kBFR8oGEO2+DmUcn5qi1AvhwpJ9o3W186MbuaiKRDa7fHis1dWC9/6
K1SovP9nmw==
In-Reply-To: <db65ddf5-4946-a84f-e064-41e6e37b874d@Damon-Family.org>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <NRUN9Tc--3-9@tutanota.com>
X-Mailman-Original-References: <NRU0CaG--3-9@tutanota.com>
<db65ddf5-4946-a84f-e064-41e6e37b874d@Damon-Family.org>
 by: Jen Kris - Sun, 26 Mar 2023 19:24 UTC

Thanks to Richard Damon and Peter Holzer for your replies.  I'm working through the call chain to understand better so I can post a followup question if needed. 

Thanks again.

Jen

Mar 26, 2023, 19:21 by Richard@Damon-Family.org:

> On 3/26/23 1:43 PM, Jen Kris via Python-list wrote:
>
>> The base class:
>>
>>
>> class Constraint(object):
>>
>> def __init__(self, strength):
>>         super(Constraint, self).__init__()
>>         self.strength = strength
>>
>> def satisfy(self, mark):
>>         global planner
>>         self.choose_method(mark)
>>
>> The subclass:
>>
>> class UrnaryConstraint(Constraint):
>>
>> def __init__(self, v, strength):
>>         super(UrnaryConstraint, self).__init__(strength)
>>         self.my_output = v
>>         self.satisfied = False
>>         self.add_constraint()
>>
>>     def choose_method(self, mark):
>>         if self.my_output.mark != mark and \
>>            Strength.stronger(self.strength, self.my_output.walk_strength):
>> self.satisfied = True
>>         else:
>>             self..satisfied = False
>>
>> The base class Constraint doesn’t have a "choose_method" class method, but it’s called as self.choose_method(mark) on the final line of Constraint shown above.
>>
>> My question is:  what makes "choose_method" a method of the base class, called as self.choose_method instead of UrnaryConstraint.choose_method?  Is it super(UrnaryConstraint, self).__init__(strength) or just the fact that Constraint is its base class?
>>
>> Also, this program also has a class BinaryConstraint that is also a subclass of Constraint and it also has a choose_method class method that is similar but not identical:
>>
>> def choose_method(self, mark):
>>     if self.v1.mark == mark:
>>             if self.v2.mark != mark and Strength.stronger(self.strength, self.v2.walk_strength):
>>                 self.direction = Direction.FORWARD
>>             else:
>>                 self.direction = Direction.BACKWARD
>>
>> When called from Constraint, it uses the one at UrnaryConstraint.  How does it know which one to use?
>>
>> Thanks,
>>
>> Jen
>>
>
> Perhaps the key point to remember is that when looking up the methods on an object, those methods are part of the object as a whole, not particually "attached" to a given class. When creating the subclass typed object, first the most base class part is built, and all the methods of that class are put into the object, then the next level, and so on, and if a duplicate method is found, it just overwrites the connection. Then when the object is used, we see if there is a method by that name to use, so methods in the base can find methods in subclasses to use.
>
> Perhaps a more modern approach would be to use the concept of an "abstract base" which allows the base to indicate that a derived class needs to define certain abstract methods, (If you need that sort of support, not defining a method might just mean the subclass doesn't support some optional behavior defined by the base)
>
> --
> Richard Damon
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor