I cannot figure out why in the following code only one parent constructor is initialized:
class Base(object):
def __init__(self):
print "Base::Base():"
class BaseWArgs(object):
def __init__(self, arg):
print "BaseWArgs::BaseWArgs(%s):" % arg
class Composite(Base, BaseWArgs):
def __init__(self):
super(Composite,self).__init__()
In this case, only Base.init() is called. If I switch the order of inheritance parameters, only the whatever-stands-first class is initialized:
class Composite(BaseWArgs, Base):
def __init__(self):
super(Composite,self).__init__('myArg')
How I can initialize both parents with one call? Shouldn't that be the responsibility of super()
?
EDIT I think my use case is a special case of multiple inheritance. I want to create an item using defaultdict as a base type, but inheriting some additional funcitonality from a third type.
from collections import defaultdict
class Base(object):
def __init__(self):
print "Base::Base():"
super(Base,self).__init__()
self.val = 'hello world'
class Item(defaultdict, Base):
def __init__(self):
super(Item,self).__init__(int)
>>> Item()
defaultdict(<type 'int'>, {})
>>> Item().val
Traceback (most recent call last):
File "<pyshell#77>", line 1, in <module>
Item().val
AttributeError: 'Item' object has no attribute 'val'
Where does Base::Base()
get lost? Why is Base
not initialized?