I have a class that inherits from 2 other classes. These are the base classes:
class FirstBase(object):
def __init__(self, detail_text=desc, backed_object=backed_object,
window=window, droppable_zone_obj=droppable_zone_obj,
bound_zone_obj=bound_zone_object,
on_drag_opacity=on_drag_opacity):
# bla bla bla
class SecondBase(object):
def __init__(self, size, texture, desc, backed_object, window):
# bla bla bla
And this is the child:
class Child(FirstBase, SecondBase):
""" this contructor doesnt work
def __init__(self, **kwargs):
# PROBLEM HERE
#super(Child, self).__init__(**kwargs)
"""
#have to do it this TERRIBLE WAY
def __init__(self, size=(0,0), texture=None, desc="", backed_object=None,
window=None, droppable_zone_obj=[], bound_zone_object=[],
on_drag_opacity=1.0):
FirstBase.__init__(self, detail_text=desc, backed_object=backed_object,
window=window, droppable_zone_obj=droppable_zone_obj,
bound_zone_obj=bound_zone_object,
on_drag_opacity=on_drag_opacity)
SecondBase.__init__(self, size, texture, desc, backed_object, window)
I wanted to solve it all nicely with **kwargs but when I call the first commented out constructor I get TypeError: __init__() got an unexpected keyword argument 'size'.
Any ideas how I can make it work with **kwargs?
Your problem is that you only tried to use
superin the child class.If you use
superin the base classes too, then this will work. Each constructor will “eat” the keyword arguments it takes and not pass them up to the next constructor. When the constructor forobjectis called, if there are any keyword arguments left over it will raise an exception.As you can see, it works, except when you pass a bogus keyword argument: