Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

panic: can't find /


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?Cameron Simpson

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: How does a method of a subclass become a method of the base class?
Date: Mon, 27 Mar 2023 12:03:29 +1100
Lines: 103
Message-ID: <mailman.2407.1679879022.20444.python-list@python.org>
References: <NRVKuYg--7-9@tutanota.com>
<ZCDrYT9w1PHE9KRL@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de imRvxUO1t07NliWNDoPBMgTBYuBv54/br9fFxFMcaaxw==
Return-Path: <cameron@cskk.id.au>
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.018
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'def': 0.04; 'run.': 0.05;
'e.g.': 0.07; 'thing.': 0.07; 'used.': 0.07; 'apparently': 0.09;
'didn\xe2\x80\x99t': 0.09; 'effectively': 0.09; 'methods,': 0.09;
'ok,': 0.09; 'subject:class': 0.09; 'typically': 0.09; 'cheers,':
0.11; 'cameron': 0.16; 'classes,': 0.16; 'found.': 0.16;
'from:addr:cs': 0.16; 'from:addr:cskk.id.au': 0.16;
'from:name:cameron simpson': 0.16; 'init': 0.16; 'inspect': 0.16;
'instance': 0.16; 'message-id:@cskk.homeip.net': 0.16;
'question,': 0.16; 'received:10.10': 0.16; 'rise': 0.16;
'simpson': 0.16; 'skip:> 10': 0.16; 'skip:> 20': 0.16; 'step.':
0.16; 'subject:does': 0.16; 'think.': 0.16; 'wrote:': 0.16;
'python': 0.16; 'says': 0.17; 'uses': 0.19; 'calls': 0.19;
'to:addr:python-list': 0.20; 'skip:_ 10': 0.22; 'code': 0.23;
'lines': 0.23; 'subject:How': 0.23; 'saying': 0.25; 'stuff': 0.25;
'brings': 0.26; "isn't": 0.27; 'function': 0.27; 'old': 0.27;
'done': 0.28; 'header:User-Agent:1': 0.30; 'looked': 0.31;
'program': 0.31; "doesn't": 0.32; 'answers': 0.32; 'but': 0.32;
'mean': 0.34; 'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34;
'running': 0.34; 'one.': 0.35; 'final': 0.35; "we're": 0.35;
'special': 0.37; 'using': 0.37; "it's": 0.37; 'class': 0.37;
'single': 0.39; 'list': 0.39; 'methods': 0.39; 'something': 0.40;
'provide': 0.60; 'gave': 0.61; 'initial': 0.61; 'method': 0.61;
'morning,': 0.61; 'here.': 0.61; 'critical': 0.62; 'skip:b 10':
0.63; 'pass': 0.64; 'our': 0.64; 'full': 0.64; '8bit%:93': 0.64;
'definition': 0.64; 'needs.': 0.64; 'your': 0.64;
'received:userid': 0.66; 'entire': 0.67; 'header:Received:6':
0.67; 'skip:e 20': 0.67; 'time,': 0.67; 'choose': 0.67; 'that,':
0.67; 'during': 0.69; 'order': 0.69; '600': 0.69; '8bit%:96':
0.69; 'i\xe2\x80\x99ll': 0.69; 'sentence': 0.69; 'showed': 0.69;
'subject:method': 0.69; 'above,': 0.70; 'process,': 0.75; 'finds':
0.76; 'chain': 0.76; 'received:172.16': 0.76; 'happens': 0.84;
'attribute': 0.84; 'of...': 0.84; 'so:': 0.84; 'stuff,': 0.84;
'subject: \n ': 0.84; 'replacing': 0.91
X-RG-Spam: Unknown
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvhedrvdehuddggeefucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuuffpveftpgfvgffnuffvtfetpdfqfgfvnecuuegrihhlohhuthemucegtddtnecunecujfgurhepfffhvffukfggtggugfgjfghfsehtkeertddtreejnecuhfhrohhmpeevrghmvghrohhnucfuihhmphhsohhnuceotghssegtshhkkhdrihgurdgruheqnecuggftrfgrthhtvghrnhepkedtjeehkeeftdevveeiteekfeeludekgfevledtffetueeggfdttdegveegvdejnecukfhppedurddugeehrdduiedrgedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghlohepsghorhhgrdgtshhkkhdrhhhomhgvihhprdhnvghtpdhinhgvthepuddrudeghedrudeirdegtddpmhgrihhlfhhrohhmpegtrghmvghrohhnsegtshhkkhdrihgurdgruhdpnhgspghrtghpthhtohepuddprhgtphhtthhopehphihthhhonhdqlhhishhtsehphihthhhonhdrohhrghdprghuthhhpghushgvrheptghskhhksegsihhgphhonhgurdgtohhmpdhgvghokffrpeetfgdpoffvtefjohhsthepnhhsshhtlhhrghduudhpqdhsvhgt
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-RG-VS-CLASS: clean
X-Authentication-Info: Submitted using ID cskk@bigpond.com
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <NRVKuYg--7-9@tutanota.com>
User-Agent: Mutt/2.2.7 (2022-08-07)
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: <ZCDrYT9w1PHE9KRL@cskk.homeip.net>
X-Mailman-Original-References: <NRVKuYg--7-9@tutanota.com>
 by: Cameron Simpson - Mon, 27 Mar 2023 01:03 UTC

On 27Mar2023 01:53, Jen Kris <jenkris@tutanota.com> wrote:
>Thanks for your reply.  You are correct about the class definition
>lines – e.g. class EqualityConstraint(BinaryConstraint).  I didn’t post
>all of the code because this program is over 600 lines long.  It's
>DeltaBlue in the Python benchmark suite. 

Doubtless. But the "class ...:" line is the critical thing.

>I’ve done some more work since this morning, and now I see what’s happening.  But it gave rise to another question, which I’ll ask at the end. 
>
>The call chain starts at
>
>    EqualityConstraint(prev, v, Strength.REQUIRED) 
>
>The class EqualityConstraint is a subclass of BinaryConstraint.  The entire class code is:
>
>    class EqualityConstraint(BinaryConstraint):
>        def execute(self):
>            self.output().value = self.input().value

Ok, so it is effectively a BinaryConstraint with a single customised
execute() method.

>Because EqualityConstraint is a subclass of BinaryConstraint, the init method of BinaryConstraint is called first.

That is courtesy of the MRO: EqualityConstraint has no __init__, so the
one found in BinaryConstraint is found and used.

>During that initialization (I showed the call chain in my previous
>message), it calls choose_method.  When I inspect the code at
>"self.choose_method(mark):" in PyCharm, it shows:
>
>    <bound method BinaryConstraint.choose_method of <Trans_01_DeltaBlue.EqualityConstraint object at 0x7f7ac09c6ee0>>

This says that it found found the choose_method method in the
BinaryConstraint class definition, but that it is _bound_ to the
EqualityConstraint instance you're using (self).

>As EqualityConstraint is a subclass of BinaryConstraint it has bound the choose method from BinaryConstraint, apparently during the BinaryConstraint init process, and that’s the one it uses.  So that answers my original question. 

Incorrectly, I think.

__init__ does _nothing_ about other methods, if you mean "__init__" in
the sentence above.

The class definition results in an EqualityConstraint.__mro__ list of
classes, which is where _every_ method is looked for _at call time_.

>But that brings up a new question.  I can create a class instance with
>x = BinaryConstraint(),

That makes an instance of EqualityConstraint.

>but what happens when I have a line like "EqualityConstraint(prev, v,
>Strength.REQUIRED)"?

That makes an instance of EqualityConstraint.

>Is it because the only method of EqualityConstraint is execute(self)?

I don't know what you're asking here.

>Is execute a special function like a class __init__?

No. It's just a method.

In a sense, __init__ isn't very special either. It is just _called_
during new instance setup as the final step. By that time, self is a
fully set up instance of... whatever and the __init__ function is just
being called to do whatever initial attribute setup every instance of
that class needs.

Python doesn't magicly call _all_ the __init__ methods of all the
superclasses; it calls the first found. So for an EqualityConstraint it
finds the one from BinaryConstraint because EqualityConstraint does not
provide an overriding one of its own.

If EqualityConstraint _did_ provide one, it might typically be
structured as:

class EqualityConstraint(BinaryConstraint):
def __init__(self, prev, v, strength, something_extra):
super().__init__(prev, v, strength)
self.extra = something_extra

So:
- Python only calls _one_ __init__ method
- in the example above, it finds one in EqualityConstraint and uses it
- because Python calls _only_ that, in order to _also_ do the normal
setup a BinaryConstraint needs, we pass the parameters used by
BinaryConstraint to the new __init__ up the chain by calling
super().__init__(BinaryConstraint-parameters-here)
- we do our own special something with the something_extra

That super().__init__() call is the _only_ thing which arranges that
superclass inits get to run. This givens you full control to sompletely
replace some superclass' init with a custom one. By calling
super().__init__() we're saying we not replacing that stuff, we're
running the old stuff and just doing something additional for our
subclass.

Cheers,
Cameron Simpson <cs@cskk.id.au>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor