Multiple Inheritance and super() (was Re: [Merge] lp:~vila/bzr/389648-hook-calls-base into lp:~bzr/bzr/trunk)
Ivan Sagalaev
maniac at softwaremaniacs.org
Sun Nov 8 00:10:31 GMT 2009
John Arbash Meinel wrote:
> class Mixin1(object):
> def __init__(self, keyword1=None, **kwargs):
> super(Mixin1, self).__init__(keyword1=keyword1, **kwargs)
> self.keyword1 = keyword1
Ah, that should fail, yes... Honestly I've never seen it done like that.
If a descendant is introducing a new keyword argument there's no point
in passing it up the inheritance tree because nobody there cares about
it. The descendant should handle it and pass only "other things" (*args,
**kwargs).
> Consider this case:
>
> class Mixin1(object):
> def __init__(self, keyword1=None, **kwargs):
>
> class Mixin2(object):
> def __init__(self, keyword2=None, **kwargs):
>
> class Mixin3(object):
> def __init__(self, keyword1=None, **kwargs):
I see. The case where two mixins clash on an argument name is
unfortunate and indeed has no nice solution. If you control the code of
all the mixins you could just avoid this situation. If it's a
third-party code you should write a violent letter to its maintainer
:-). And while he's busy updating the code you can temporarily wrap one
of the mixins into an adapter renaming the offending argument:
class Mixin3Wrapper(Mixin3):
def __init__(self, keyword3=None, **kwargs):
super(Mixin3Wrapper, self).__init__(keyword1=keyword3,
**kwargs)
And use it instead of a pure Mixin3.
More information about the bazaar
mailing list