Change the signature of a function called in __init__ of the base class

I have a class A. In __init__ AI call the method self.func(argument).

class A(object):
    def __init__(self, argument, key=0):
        self.func(argument)

A class Bis a subclass A. In BI want to change the signature func. It is called when I call the constructor Afrom B.

class B(A):
    def __init__(self, argument1, argument2, key=0):
        super(B, self).__init__(argument1, key) # can't send argument 2 to __init__ of A

    def func(self, argument1, argument2):
        #some code here

Clearly this does not work because the signature funcexpects 2 arguments. How do I get around this? Or is there something wrong with the way I designed my classes?

keyis the default argument for __init__for A. argument2not intended for key. argument2is an optional argument that B takes, but A does not. B also accepts keyand has a default value for it.

, A. key 0. , A(arg), A(arg, key=0).

+4
3

, , 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>
+4

, . , , , . , Parent.method .

>>> class Parent:
    def __init__(self, a, b=None):
        Parent.method(self, a)
        self.b = b
    def method(self, a):
        self.location = id(a)


>>> class Child(Parent):
    def __init__(self, a):
        super().__init__(a, object())
    def method(self, a, b):
        self.location = id(a), id(b)


>>> test = Child(object())

, , . -. .

0

__init__ func False B __init__

class A(object):
    def __init__(self, argument, key=0, call_func=True):
        if call_func:
            self.func(argument)

class B(A):
    def __init__(self, argument):
        argument1, argument2 = argument, 'something else'
        super(B, self).__init__(argument1, argument2, call_func=False)
0

All Articles