I am trying to write an __init__
method for a class such that all the classes it derives from get the same set of keyword arguments. Let's say,
class First(object):
def __init__(self, *args, **kwargs):
super(First, self).__init__()
print('First', kwargs)
class Second(object):
def __init__(self, *args, **kwargs):
super(Second, self).__init__()
print('Second', kwargs)
class Third(First, Second):
def __init__(self, *args, **kwargs):
super(Third, self).__init__(*args, **kwargs)
print('Third', kwargs)
If I instantiate Third
with a bunch of keywords, I can see that they all get "taken out" by the __init__
method of First
,
In [5]: tc = Third(key='value', someother='nothing')
Second {}
First {'key': 'value', 'someother': 'nothing'}
Third {'key': 'value', 'someother': 'nothing'}
I understand that this is the expected behavior, so I'm not complaining. However, is there a way to write this class hierarchy such that both First
and Second
get the same set of kwargs
? More generally, I'd like to write the hierarchy such that if Third
derives from N
classes, all N
of those should get the same set of kwargs
unless one of the explicitly pops one of the keys.
I have looked at other questions such as this one, and answers have pointed out that that's not the default beahviour. I get that. But, is there a way to do it?