Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

It is not best to swap horses while crossing the river. -- Abraham Lincoln


devel / comp.lang.python / Multiple inheritance and a broken super() chain

SubjectAuthor
o Multiple inheritance and a broken super() chainPeter Slížik

1
Multiple inheritance and a broken super() chain

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: peter.sl...@gmail.com (Peter Slížik)
Newsgroups: comp.lang.python
Subject: Multiple inheritance and a broken super() chain
Date: Mon, 3 Jul 2023 19:38:08 +0200
Lines: 59
Message-ID: <mailman.155.1688405907.23016.python-list@python.org>
References: <CAC-uhUARYKsk92r4BEDmDGx6wbyLZbrOeL1izwHurOUDjEbJJA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de mIOWX4zYqfM93E9AIC/RQwL0I9eFyWLdbzEsVwvEDEcA==
Return-Path: <peter.slizik@gmail.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=gmail.com header.i=@gmail.com header.b=ZbBfE5rx;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.172
X-Spam-Level: *
X-Spam-Evidence: '*H*': 0.66; '*S*': 0.01; 'def': 0.04; 'parent':
0.07; 'view,': 0.07; 'example.': 0.09; 'textbook': 0.09;
'trivial': 0.09; 'classic': 0.16; 'examine': 0.16; 'init': 0.16;
'intent': 0.16; 'legacy': 0.16; 'once.': 0.16; 'right.': 0.16;
'subject:() ': 0.16; 'subject:super': 0.16; 'top,': 0.16; 'uses':
0.19; 'calls': 0.19; 'to:addr:python-list': 0.20; 'written': 0.22;
'skip:_ 10': 0.22; "what's": 0.22; 'code': 0.23; 'skip:p 30':
0.23; 'behavior': 0.26; 'classes': 0.26; 'it,': 0.29; 'point,':
0.32; 'right,': 0.32; 'skip:s 80': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm": 0.33; 'there':
0.33; 'received:google.com': 0.34; 'complex': 0.35;
'from:addr:gmail.com': 0.35; 'thanks,': 0.36; 'class': 0.37;
'though': 0.37; 'two': 0.39; 'single': 0.39; 'this,': 0.39; 'use':
0.39; 'break': 0.39; 'both': 0.40; 'here': 0.62; 'skip:b 20':
0.63; 'skip:m 20': 0.63; 'everything': 0.63; 'skip:b 10': 0.63;
'from:charset:utf-8': 0.64; 'skip:r 20': 0.64; 'top': 0.65; 'let':
0.66; 'decided': 0.67; 'now,': 0.67; 'worked': 0.67; 'right':
0.68; 'before,': 0.69; 'manually': 0.69; 'order.': 0.69; 'sans':
0.69; 'skip:* 10': 0.71; '<span': 0.76; 'chain': 0.76; 'parents':
0.76; 'out.': 0.80; 'left': 0.83; 'chains,': 0.84; 'diamond':
0.84; 'inheritance': 0.84; 'parent.': 0.84; 'subject:inheritance':
0.84; 'surprised': 0.84; 'era,': 0.91; 'me:': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1688405904; x=1690997904;
h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
:date:message-id:reply-to;
bh=LBtUDQDrJBGw9vYu3Q523Lp3szvJkO11JGITgMhdvdg=;
b=ZbBfE5rx+rQnM6uK+i0uHl5kIP9MsT2nvYwF21Qt4ww/oepmgy1O5E9/yHgek2LMMt
kGBKNr3UApwotbFBIESd2c7K+Lr8uleOEfcyYwctRY0Ta3SRVgra+b6Q7uZEYCVxDo3I
MTJXAJIPM4bqzfWx7eV3TsG3KSHuiO2/XMpTaBSGW+g/oKSPeqkZOPoXTQ8XPBBDX/T5
AlJY1PwGDQHVOds7+/L427eH9oQZ2I6Et0fIF+Kq1t7a+odd0wkumog+BcM+1krZAEoL
VKq+Uwl2tSWEeeeTnrc8B1rcb6RFCGCdtzVoeVCYR9hyCF8166RakxFOfMInTXeZFOpO
SulA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1688405904; x=1690997904;
h=to:subject:message-id:date:from:mime-version:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=LBtUDQDrJBGw9vYu3Q523Lp3szvJkO11JGITgMhdvdg=;
b=iBPt1C6H6nXKoBdCCO4M1gLld3XPTnm8xoIVSSHgkXskWyzoa7ZCBP/8z1Z+9pNn+U
/NNbgcygCY0JqCjdvnQRbI4gn9w/AquGUrmHGswMZ+/m3AkaPE+oB//JDBCjN9JkW59N
dFKSajYGaz5U3fdS0BtnDtCxaZTJ8ODhpDPUIAQ+FGsY59EneU1vA5FJXBWKAwM4foYY
jePppjqRXwaEU7zumtYWpH7yNv5LYnYLGgPiyVPMDHtDT0GbQrKMprAJ7E+mkLGqnsid
I03XkRlIB0B5V7Qa5H+4BrVz54t4j6oV2OA6qCQaXUZGZuvQU4iHLyHbbcSn0BcFxwI8
ES8A==
X-Gm-Message-State: ABy/qLYqDDCvjAysXsTmJ2xlINBd3TDSDpBlEWk5Udnr4REarKPRttJ9
HHhJlEgX4PboSvUN4Ejk8XP4hfO4IdrptoNzElPuihl7MKU=
X-Google-Smtp-Source: APBJJlG8TPHET19F4K3BIDOVruLAhV8LShRdOG2hY32yoiGvzJ+sT5Ls/TWvwEQZ1Ofjx3q19z3ifTRwCt8sYIPj8YU=
X-Received: by 2002:a05:6358:9d81:b0:134:f070:d6b8 with SMTP id
d1-20020a0563589d8100b00134f070d6b8mr7973378rwo.4.1688405904167; Mon, 03 Jul
2023 10:38:24 -0700 (PDT)
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: <CAC-uhUARYKsk92r4BEDmDGx6wbyLZbrOeL1izwHurOUDjEbJJA@mail.gmail.com>
 by: Peter Slížik - Mon, 3 Jul 2023 17:38 UTC

Hello.

The legacy code I'm working with uses a classic diamond inheritance. Let me
call the classes *Top*, *Left*, *Right*, and *Bottom*.
This is a trivial textbook example. The classes were written in the
pre-super() era, so all of them initialized their parents and Bottom
initialized both Left and Right in this order.

The result was expected: *Top* was initialized twice:

Top.__init__() Left.__init__() Top.__init__() Right.__init__()
Bottom.__init__()

Now I replaced all parent init calls with *super()*. After this, Top was
initialized only once.

Top.__init__() Right.__init__() Left.__init__() Bottom.__init__()

But at this point, I freaked out. The code is complex and I don't have the
time to examine its inner workings. And before, everything worked correctly
even though Top was initialized twice. So I decided to break the superclass
chain and use super() only in classes inheriting from a single parent. My
intent was to keep the original behavior but use super() where possible to
make the code more readable.

class Top:
def __init__(self):
print("Top.__init__()")

class Left(Top):
def __init__(self):
super().__init__()
print("Left.__init__()")

class Right(Top):
def __init__(self):
super().__init__()
print("Right.__init__()")

class Bottom(Left, Right):
def __init__(self):
Left.__init__(self) # Here I'm calling both parents manually
Right.__init__(self)
print("Bottom.__init__()")

b = Bottom()

The result has surprised me:

Top.__init__() Right.__init__() Left.__init__() Top.__init__()
Right.__init__() Bottom.__init__()

Now, as I see it, from the super()'s point of view, there are two
inheritance chains, one starting at Left and the other at Right. But
*Right.__init__()* is called twice. What's going on here?

Thanks,
Peter

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor