oop - Python multiple inheritance constructor not called when using super() -
consider following code:
class a(object): def __init__(self): pass class b(object): def __init__(self): self.something = 'blue' def get_something(self): return self.something class c(a,b): def __init__(self): super().__init__() print(self.get_something()) and do:
c = c() which results in this:
attributeerror: 'c' object has no attribute 'something' i suppose happens due constructor of b not being called when using super(). there way achieve correct behavior python 3?
superclasses should use super if subclasses do. if add super().__init__() line , b example should work again.
check method resolution order of c:
>>> c.mro() [__main__.c, __main__.a, __main__.b, builtins.object] this article should clear things up.
Comments
Post a Comment