In [5]: class a(object):
...: def __init__(self):
...: print "In class a"
...: self.a = 1
...:
In [6]: class b(object):
...: def __init__(self):
...: print "In class b"
...: self.b = 2
...:
...:
In [7]: class c(b, a):
...: pass
...:
In [8]: c.mro()
Out[8]:
[<class '__main__.c'>,
<class '__main__.b'>,
<class '__main__.a'>,
<type 'object'>]
In [9]: obj = c()
In class b
In [10]: obj.__dict__
Out[10]: {'b': 2}
The default __init__
method of class c
is called on obj
creation, which internally calls the __init__
of only class b
.
As per my understanding, if I inherit from 2 class, my derived class object should have variables from both class (unless they are private to those classes).
My Question: Am I wrong in expecting my derived object to contain variables from both classes? If so, why? Shouldn't __init__
of class a
also be called? What would have happened in language like C++?