Python method of child with less input, best practice

Viewed 43

I have two classes, A and and it's child B. The __init__ of A uses same_method(*some_input). In B this method should be adjusted to take no input. (This is a simplified example).

class A():
    def __init__(self, *some_input) -> None:
        self.same_method(*some_input)

    def same_method(self, *some_input):
        print(*some_input)


class B(A):
    def __init__(self) -> None:
        super().__init__()

    def same_method(self):
        print('This is B')

Expected output:

This is A
This is B

This example works fine. If the method is renamed to __same_method however the output is:

This is A

It seems that it runs A.__same_method in B.__init__.

As my main project contains many Dunder methods and I only recently introduced inheritance I was wondering if there is a way to make this work with Dunder methods as well. Or is this something I really should not do? Whats the best practice for child methods with a different signature?

(I'm using Python 3.9 with PyCharm 2021.2.3)

0 Answers
Related