[Python] How to share arguments in different classes?

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024.

  1. Stack

    Stack Membro Participativo

    Suppose I have a python class with arguments in the __init__ method with types specified.

    Class A:
    def __init__(self, arg_1:int, ..., arg_n:int):
    pass


    Suppose I inherit from it as such:

    Class B(A):
    def __init__(self, arg_2:int, ..., arg_n:int):
    super().__init__(arg1="str", arg_2, ..., arg_n)


    The problem here is that I have to write the the whole argument list including the types. If I decide to change the type of one argument to some other type, I have to do so for all subclasses. How can we avoid this?

    One possible solution could be the one below. However, the constructor here does not show the list of arguments as they are defined in Class A.

    Class B(A):
    def __init__(self, **kwargs):
    super().__init__(arg1="str", **kwargs)


    Is it maybe possible to group the common arguments in one class and share that in all classes?

    Continue reading...

Compartilhe esta Página