Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

My sister opened a computer store in Hawaii. She sells C shells down by the seashore.


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?Peter J. Holzer

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-pyt...@hjp.at (Peter J. Holzer)
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 20:48:25 +0200
Lines: 101
Message-ID: <mailman.2401.1679856876.20444.python-list@python.org>
References: <NRU0CaG--3-9@tutanota.com>
<20230326184825.vjb5zwhi3elrfxsw@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="sgzyhqpiqot5fnbg"
X-Trace: news.uni-berlin.de 6g6y/kR2lsKBJSLoBdmsfwnCev6ZRsmWI1hqxFB9+DNA==
Return-Path: <hjp-python@hjp.at>
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.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'class,':
0.05; 'content-type:multipart/signed': 0.05; 'class.': 0.07;
'above.\xc2\xa0': 0.09; 'content-type:application/pgp-signature':
0.09; 'else:': 0.09; 'filename:fname piece:asc': 0.09;
'filename:fname piece:signature': 0.09;
'filename:fname:signature.asc': 0.09; 'skip:x 10': 0.09;
'skip:\xc2 20': 0.09; 'subject:class': 0.09; '"creative': 0.16;
'+0200,': 0.16; '__/': 0.16; 'challenge!"': 0.16; 'constraint,':
0.16; 'from:addr:hjp-python': 0.16; 'from:addr:hjp.at': 0.16;
'from:name:peter j. holzer': 0.16; 'hjp@hjp.at': 0.16; 'holzer':
0.16; 'implements': 0.16; 'inspecting': 0.16; 'nothing.': 0.16;
'reality.': 0.16; 'stross,': 0.16; 'subject:does': 0.16; 'url-
ip:212.17.106.137/32': 0.16; 'url-ip:212.17.106/24': 0.16; 'url-
ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16; 'wrote:':
0.16; 'instead': 0.17; 'uses': 0.19; 'to:addr:python-list': 0.20;
'subject:How': 0.23; 'object': 0.26; "isn't": 0.27; 'fact': 0.28;
'sense': 0.28; 'program': 0.31; 'question': 0.32; 'python-list':
0.32; 'mark': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34;
'final': 0.35; 'also,': 0.36; 'member': 0.37; 'class': 0.37;
'skip:u 20': 0.39; 'base': 0.40; 'something': 0.40; 'method':
0.61; "there's": 0.61; 'skip:\xc2 10': 0.62; 'received:212': 0.62;
'true': 0.63; 'skip:b 10': 0.63; 'similar': 0.65;
'received:userid': 0.66; 'ps:': 0.69; 'subject:method': 0.69;
'url-ip:212/8': 0.69; '8bit%:6': 0.71; 'global': 0.73;
'8bit%:100': 0.76; 'method,': 0.84; 'received:at': 0.84; 'subject:
\n ': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <NRU0CaG--3-9@tutanota.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: <20230326184825.vjb5zwhi3elrfxsw@hjp.at>
X-Mailman-Original-References: <NRU0CaG--3-9@tutanota.com>
 by: Peter J. Holzer - Sun, 26 Mar 2023 18:48 UTC
Attachments: signature.asc (application/pgp-signature)

On 2023-03-26 19:43:44 +0200, Jen Kris via Python-list wrote:
> The base class:
>
>
> class Constraint(object):
[...]
> def satisfy(self, mark):
>         global planner
>         self.choose_method(mark)
>
> The subclass:
>
> class UrnaryConstraint(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,

Nothing. choose_method isn't 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? 

This works only if satisfy() is called on a subclass of Constraint which
actually implements this method.

If you do something like

x = UrnaryConstraint()
x.satisfy(whatever)

Then x is a member of class UrnaryConstraint and will have a
choose_method() method which can be called.

> 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:
...
> When called from Constraint, it uses the one at UrnaryConstraint.  How
> does it know which one to use? 

By inspecting self. If you call x.satisfy() on an object of class
UrnaryConstraint, then self.choose_method will be the choose_method from
UrnaryConstraint. If you call it on an object of class BinaryConstraint,
then self.choose_method will be the choose_method from BinaryConstraint.

hp

PS: Pretty sure there's one "r" too many in UrnaryConstraint.

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor