Python: Mixing super and classic Calls
От: sinmaster  
Дата: 16.09.10 04:55
Оценка:
доброе утро.
цитата:

In the following example, a C class that calls its base classes using the __init__ method will
make B class be called twice!
>>> class A(object):
... def __init__(self):
... print "A"
... super(A, self).__init__()
...
>>> class B(object):
... def __init__(self):
... print "B"
... super(B, self).__init__()
...
>>> class C(A,B):
... def __init__(self):
... print "C"
... A.__init__(self)
... B.__init__(self)
...
>>> print "MRO:", [x.__name__ for x in C.__mro__]
MRO: ['C', 'A', 'B', 'object']
>>> C()
C A B B
<__main__.C object at 0xc4910>

This happens due to the A.__init__(self) call, which is made with the C instance,
thus making super(A, self).__init__() call B's constructor.
In other words,
super should be used into the whole class hierarchy. The problem is that sometimes
a part of this hierarchy is located in third-party code.

мораль басни понял — не смешивать старый и новый способы вызова родительского "конструктора", но объяснение происходящего, выделенное жирным не понимаю. поясните, плз?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.