another super wart
Michele Simionato
mis6 at pitt.edu
Fri Mar 14 11:44:57 EST 2003
SOLVED!!
Finally, after quite a lot of thought, I have understood how does
it work. There is no bug, here, super is OK and also __new__ is OK.
I was mislead by a wrong intuition.
The hierarchy I have used in my original posting is the following:
5
object
/ |
2 A type 4
/ |
/ |
1 B--. Type 3
\ \ /
\ M 0
\ :
C
The colon in the graph denotes instantiation, i.e. C is an instance of M.
On the same time, C inherits from B and M inherits from B and Type.
The point is that C is both a subclass of B and an instance of M.
This generates an ambiguity in super with two possible interpretations
(and only one correct):
1. First interpretation (the wrong one!).
super(B,C) is interpreted as "the superclass of B with respect to the
MRO of C".
According to this interpretation, the superclass of B is A and therefore
super(B,C).__new__ calls A.__new__ which is object.__new__: but this is
not true!
2. Second interpretation (the correct one!)
super(B,C) is interpreted as "the superclass of B with respect to the
MRO of type(C)". Now the MRO of type(C)=M is
MRO[M]=[M B A Type type object]
therefore
super(B,C).__new__ calls A.__new__ which is Type.__new__ and not
object.__new__!
My example with a staticmethod called new defined both in A and in Type
was misleading, since the wrong interpretation was giving the same
result of the right one:
super(B,C).new calls A.new that overrides Type.new.
All the trouble came because I did not understand how super works, but
now I am happy ;)
Michele
More information about the Python-list
mailing list