Say, I have a class Foo, extending class Bar. And I want to slightly override Foo's consructor. And I don't want even know what signarure of Bar's constructors is. Is there a way to do this?
If you didn't understand, I mean the following:
class Bar:
def __init__ (self, arg1=None, arg2=None, ... argN=None):
....
class Foo (Bar):
#Here i just want add additional parameter to constructor, but don't want to know anything about Bar's other parameters (arg1, arg2..., argN)
def __init__ (self, my_new_arg=None, ??? )
self.new_arg = my_new_arg
Bar.__init__(self, ??? )
Is there a way to put something short and elegant instead of ??? in this code?
(Maybe some variations of args/kwargs)
解决方案
class Parent(object):
def __init__(self, a, b):
print 'a', a
print 'b', b
class Child(Parent):
def __init__(self, c, d, *args, **kwargs):
print 'c', c
print 'd', d
super(Child, self).__init__(*args, **kwargs)
test = Child(1,2,3,4)
Output:
c 1
d 2
a 3
b 4