Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Mommy, what happens to your files when you die?


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

SubjectAuthor
* How does a method of a subclass become a method of the base class?Jen Kris
+- Re: How does a method of a subclass become a method of the base class?Stefan Ram
`- Re: How does a method of a subclass become a method of the baseaapost

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jenk...@tutanota.com (Jen Kris)
Newsgroups: comp.lang.python
Subject: How does a method of a subclass become a method of the base class?
Date: Sun, 26 Mar 2023 19:43:44 +0200 (CEST)
Lines: 68
Message-ID: <mailman.2400.1679853208.20444.python-list@python.org>
References: <NRU0CaG--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 QLqBZ1/Brr+PsqM3b72/swnwy+NRh8tyjdYQC9VPIAdQ==
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=MEXHwra8;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.021
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'def': 0.04; 'class,':
0.05; 'skip:\xc2 30': 0.07; 'above.\xc2\xa0': 0.09; 'else:': 0.09;
'skip:\xc2 20': 0.09; 'subject:class': 0.09; 'constraint,': 0.16;
'subject:does': 0.16; 'use?': 0.16; 'instead': 0.17; 'uses': 0.19;
'to:addr:python-list': 0.20; 'skip:_ 10': 0.22; 'subject:How':
0.23; 'received:de': 0.23; 'fact': 0.28; 'program': 0.31;
'question': 0.32; 'mark': 0.32; 'received:192.168.1': 0.32; 'but':
0.32; 'final': 0.35; 'also,': 0.36; 'thanks,': 0.36; 'class':
0.37; 'received:192.168': 0.37; 'skip:u 20': 0.39; 'base': 0.40;
'method': 0.61; 'skip:\xc2 10': 0.62; 'true': 0.63; 'skip:b 10':
0.63; 'similar': 0.65; 'strength': 0.69; 'subject:method': 0.69;
'8bit%:6': 0.71; 'global': 0.73; '8bit%:100': 0.76; 'method,':
0.84
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1679852624;
s=s1; d=tutanota.com;
h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Date:Date:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:Sender;
bh=gGl+FIvBuDQy2S4/02+SjmBBokJRkEh3I0N2QR5LTcg=;
b=MEXHwra8RbPe/WU+87zZwRiIP6p9q4FeZMjWHeib4AM0nT1vr1BPAJrxJY7VMvr5
Dhia+oX+RYzr6pxDL5sPWV09kIU4vSccPTC7Mk5dlh/ZM99vm7re5qhIMn+PI/rE895
eWVCmkSZ10X13WCV5nKNfkd4w2HQKYrntXKdRb+a2Eo6zsPvIsrNpEnpbnhZ9lynJJ/
GYmfFJ/wIojl3oz/I0MaOHMHIxX6W5s97ZHt0uPz8G4QmpQwBbmYtD2Xorpk54X59wT
tPlxfm4DER7Oj8GHo8auJGXblh/X+7zugmsa5JxFBeAbvwRZboo8Yx6lxtlg/lqjeUu
xV2rJ0JsCg==
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: <NRU0CaG--3-9@tutanota.com>
 by: Jen Kris - Sun, 26 Mar 2023 17:43 UTC

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

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

<subclass-20230326185712@ram.dialup.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: How does a method of a subclass become a method of the base class?
Date: 26 Mar 2023 18:06:59 GMT
Organization: Stefan Ram
Lines: 33
Expires: 1 Apr 2024 11:59:58 GMT
Message-ID: <subclass-20230326185712@ram.dialup.fu-berlin.de>
References: <mailman.2400.1679853208.20444.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de lLmHbRC4eqh03F3c8Hd8oAgLZshO5SF1n1Xs5ajHhSsISj
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR
 by: Stefan Ram - Sun, 26 Mar 2023 18:06 UTC

Jen Kris <jenkris@tutanota.com> writes:
>My question is: what makes "choose_method" a method of the base class=
>, called as self.choose_method instead of UrnaryConstraint.choose_method?

You can see that in the following code "b()" is called on the object
"self" in the base class "A", even though there is no "def b" in the
base class A:

class A:
def a( self ):
return self.b()

class B( A ):
def b( self ):
return "I am a string in B.b."

print( B().a() )

print( A().a() )

. Please look at the following transcript of the execution of this
code:

I am a string in B.b.
AttributeError: 'A' object has no attribute 'b'

. Now, your question should be:

|What makes "b" be a methode of "B()", but not of "A()"?

. And maybe now you can answer it yourself!

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

<tvq3m6$kho$1@nnrp.usenet.blueworldhosting.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!nnrp.usenet.blueworldhosting.com!.POSTED!not-for-mail
From: aap...@idontexist.club (aapost)
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 14:42:14 -0400
Organization: BlueWorld Hosting Usenet (https://usenet.blueworldhosting.com)
Message-ID: <tvq3m6$kho$1@nnrp.usenet.blueworldhosting.com>
References: <NRU0CaG--3-9@tutanota.com>
<mailman.2400.1679853208.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 26 Mar 2023 18:42:14 -0000 (UTC)
Injection-Info: nnrp.usenet.blueworldhosting.com;
logging-data="21048"; mail-complaints-to="usenet@blueworldhosting.com"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
Cancel-Lock: sha1:itF+8Jllwb0iv0lBzCAWOAy1VWs= sha256:iD2Bn1AYqbKEc0aImWSvtHJBt/5MfOYA3Zt3TnBsCD8=
sha1:2amZi2WtTlt8qMprwfLX6S/2/ss= sha256:dJxoxTF0g2R0PdiOeN5KvQ5HJXuqJjEVDdSxmpsuFvw=
Content-Language: en-US
In-Reply-To: <mailman.2400.1679853208.20444.python-list@python.org>
 by: aapost - Sun, 26 Mar 2023 18:42 UTC

On 3/26/23 13:43, Jen Kris wrote:
>
> 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?

When referring to "self" you are referring to an "instance" of the class
or classes, think in terms of objects (the instance, usually self) vs a
blueprint (the class, usually cls).

saying self.choose_method(mark) checks the "instance" of self to see if
it has something called choose_method at run time (instances being
created at run time). If you have an instance of just Constraint, and
never had a choose_method defined for it, you will get an error because
it can't find it (which in this case is presumed as designed). If the
"instance" is of a subclass of Constraint that does have a
choose_method, that also inherits the stuff from Constraint, it will
successfully call it against the "instance" that has been constructed
with attributes of both classes.

if the instance of a subclass has a definition for something that is in
the base class, for instance IF UrnaryConstraint had it's own def
satisfy() method, the instance would call the subclass version. In the
case given, it does not, so it looks to the parent to see if it has
inherited satisfy() from higher up.

Hopefully that helps a little bit.. just have to get a feel for OO
instantiated object vs class..

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor