, , API, .
A.__init__, , self.func():
class A(object):
def __init__(self, argument, *extra, **kwargs):
key = kwargs.get('key', 0)
self.func(argument, *extra)
class B(A):
def __init__(self, argument1, argument2, key=0):
super(B, self).__init__(argument1, argument2, key=key)
, super(B, self).__init__(), extra self.func() argument.
Python 2, extra, **kwargs, key . key B key=key.
Python 3 ; *args key=0 key :
class A(object):
def __init__(self, argument, *extra, key=0):
self.func(argument, *extra)
func() a *extra, A B; - , A, B:
class A(object):
def func(self, argument, *extra):
class B(A):
def func(self, argument1, argument2, *extra):
- Python 2:
>>> class A(object):
... def __init__(self, argument, *extra, **kwargs):
... key = kwargs.get('key', 0)
... self.func(argument, *extra)
... def func(self, argument, *extra):
... print('func({!r}, *{!r}) called'.format(argument, extra))
...
>>> class B(A):
... def __init__(self, argument1, argument2, key=0):
... super(B, self).__init__(argument1, argument2, key=key)
... def func(self, argument1, argument2, *extra):
... print('func({!r}, {!r}, *{!r}) called'.format(argument1, argument2, extra))
...
>>> A('foo')
func('foo', *()) called
<__main__.A object at 0x105f602d0>
>>> B('foo', 'bar')
func('foo', 'bar', *()) called
<__main__.B object at 0x105f4fa50>