mi
Thomas Jollans
thomas at jollybox.de
Wed Aug 11 16:24:35 EDT 2010
On Wednesday 11 August 2010, it occurred to Eric J. Van der Velden to exclaim:
> Hello,
>
> I have these types,
>
> class A:
> def __init__(s):
> super().__init__()
> print("A")
> class B(A):
> def __init__(s):
> super().__init__()
> print("B")
> class C(A):
> def __init__(s):
> super().__init__()
> print("C")
> class D(B,C):
> def __init__(s):
> super().__init__()
> print("D")
>
> If I do (in 3.1)
>
> >>> d=D()
>
> A
> C
> B
> D
>
> Why this order? I thought, first to D, then B, then A. He prints "A".
> He comes back in B and prints "B". He goes to C. Then somehow he
> doesn't go again to A. He prints "C". Then back to D and prints "D".
Think again about what you're seeing here. You're printing AFTER the call to
super().__init__(). That means that it first walks the inheritance hierarchy,
and then prints -- your trace is "the wrong way around"
So, what happens is: you call D(). In it, super() delegates to B(). Which has
super delegate to C(), which then has super() delegate finally to A().
D, B, C, A
You say you were expecting D, B, A -- but what of C? You also imply that you
would have expected two visits to A -- but that would defeat the point of
super() -- you don't actually want one constructor to be called twice: the
constructor almost certainly isn't written with that possibility in mind.
>
> Thanks,
>
> Eric J.
More information about the Python-list
mailing list