super and __init__ arguments (Was: Re: Multiple inheritance with a common base class)

Duncan Booth duncan.booth at invalid.invalid
Tue Aug 10 17:26:38 CEST 2004


Markus Bertheau <twanger at bluetwanger.de> wrote in 
news:mailman.1447.1092145129.5135.python-list at python.org:

> Now how do I make the following work:
> 
> class CommonBase(object):
>     def __init__(self, c):
>         pass
>   
> class LeafA(CommonBase):
>     def __init__(self, c, a):
>         super(LeafA, self).__init__(c)
>   
> class LeafB(CommonBase):
>     def __init__(self, c, b):
>         super(LeafB, self).__init__(c)
>   
> class Multi(LeafA, LeafB):
>     def __init__(self, c, a, b):
>         super(Multi, self).__init__(c, a, b)
>   
> m = Multi(0, 1, 2)
> 

The best way is to use keyword arguments:

class CommonBase(object):
    def __init__(self, c):
        pass
  
class LeafA(CommonBase):
    def __init__(self, a, **kw):
        super(LeafA, self).__init__(**kw)
  
class LeafB(CommonBase):
    def __init__(self, b, **kw):
        super(LeafB, self).__init__(**kw)
  
class Multi(LeafA, LeafB):
    def __init__(self, **kw):
        super(Multi, self).__init__(**kw)
  
m = Multi(c=0, a=1, b=2)



More information about the Python-list mailing list