Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"The greatest warriors are the ones who fight for peace." -- Holly Near


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?Richard Damon

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: Richard@Damon-Family.org (Richard Damon)
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 15:19:47 -0400
Lines: 69
Message-ID: <mailman.2402.1679858390.20444.python-list@python.org>
References: <NRU0CaG--3-9@tutanota.com>
<db65ddf5-4946-a84f-e064-41e6e37b874d@Damon-Family.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de JXIuPNv00Dh5FFHaIaH9JwXlw/UNaSTRkLl+WNskQSVg==
Return-Path: <Richard@Damon-Family.org>
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.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'class,':
0.05; 'indicate': 0.05; 'class.': 0.07; 'skip:\xc2 30': 0.07;
'damon': 0.09; 'derived': 0.09; 'else:': 0.09; 'methods,': 0.09;
'skip:\xc2 20': 0.09; 'subject:class': 0.09; 'support,': 0.09;
'built,': 0.16; 'connection.': 0.16; 'constraint,': 0.16;
'found,': 0.16; 'from:addr:damon-family.org': 0.16;
'from:addr:richard': 0.16; 'from:name:richard damon': 0.16;
'level,': 0.16; 'message-id:@Damon-Family.org': 0.16; 'object,':
0.16; 'subject:does': 0.16; 'use?': 0.16; 'wrote:': 0.16;
'instead': 0.17; 'uses': 0.19; 'pm,': 0.19; 'to:addr:python-list':
0.20; 'skip:_ 10': 0.22; 'subject:How': 0.23; 'behavior': 0.26;
'object': 0.26; 'creating': 0.27; 'fact': 0.28; 'header:User-
Agent:1': 0.30; 'approach': 0.31; 'program': 0.31; "doesn't":
0.32; 'question': 0.32; 'concept': 0.32; 'python-list': 0.32;
'mark': 0.32; 'but': 0.32; '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; 'put': 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;
'similar': 0.65; 'strength': 0.69; 'subject:method': 0.69;
'8bit%:6': 0.71; 'global': 0.73; '8bit%:100': 0.76; 'method,':
0.84; 'subject: \n ': 0.84; 'duplicate': 0.93
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.9.0
Content-Language: en-US
In-Reply-To: <NRU0CaG--3-9@tutanota.com>
X-Provags-ID: V03:K1:/0FZcg4fth697Z+xvfWPzx1Cb8G+4AuvJ2EVuMdWUOmbncyPDE2
G6dTCU/4Jzq34UcmO91ve4vdeEMqHvw3Ovk9e2+BqrSCkODXEDG2v+8OCum3/Hu/At1ANV9
uVzbMI2+CaZ7gHTQcuw7jDXY1HKyNoNZmVfnPRf/A2sHk1gk4+0eajoRz9F4/2SxMY4Op3t
LzryEsBw6Q4zEIaOu2Egg==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:WxO3oRCBeMY=;2ne59rvzGAT3+Y4mcnMBQUYe6UM
skF+/N8sHKmXUefg/RucTycuVYrFBg21vw12fvcn0bsmBCE/XONSw7bxApIh1QUASpwacIHJi
1afslAyarBEU1veWQyPlg1R523ZwATij/cpsa3nq6jK3G/8BaXa8XzWoeu+lZEA+fBF3MtE+4
9umcQk/bi75ftKjFV987rGedzT+W30H+XL/s8jnD3M+C0lmEQZPWvQ2RBldm4aeBHSs4te0oB
Oqwbp9u6LCs2KisgAcGJHgM8xFttPyytWie+/mwprU8fZ+mR7cQXFEgb1se/tFVV0HTfpI76p
a3Hn3X/j16oQRx7BYRhj84FgswkhPNGnlCHEsyteztlYNq7Wkn0tgstf+0fucUmKuistDMx8B
EIAlM2Xx8aP+ypcOR+d5HX7pO0TJm47cY7HWdPO9k+l6irX3yyuiRvpr3uiJeNw3g1ZIQ4kyt
BobVejCScgmt9EQe7Olto4UC+WwUCzd1o1gZN/EwgxHoLu26tTvjuHkxOHi60VrGPxay8WC/D
D9fsfW2J6H8vCF7Ph3HFN6OEBGLudbAwk92TYzSbxULt1m+3hS+PapTi1gsq/11kNikTOf6ab
pFXWdYVSf7QFO5lPGR4qX6BRxflCOBHfn/tevHSp/FYnhSpNGo4DJN0bnqdLVDwCMKuagmnZg
qcA5qMVZdjvrV4LGtl3TEShMBuSi5W5v+wR6gnYDjg==
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: <db65ddf5-4946-a84f-e064-41e6e37b874d@Damon-Family.org>
X-Mailman-Original-References: <NRU0CaG--3-9@tutanota.com>
 by: Richard Damon - Sun, 26 Mar 2023 19:19 UTC

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


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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor