-1

I am pretty new to OOP in python (ver. 3.5). I created a generic class and I would like to create a new one inheriting from it.

My parent class (say A) has two args defined within its __init__() method. How can I inherit those in my child class (say B) so that when I instantiate B I can pass it the args I would pass to A? I am actually trying to use the super() function, but I am not quite sure about the result.

I tried the below, but it gives me TypeError: __init__() takes 1 positional argument but 3 were given

class A(): # parent class
    def __init__(self, x, y):
        self.x = x
        self.y = y

class B(A): # child class
    def __init__(self):
        super().__init__(x, y)

And then I would do (e.g.) b = B(5, 5)

EDIT 1

As my question was identified as duplicate of this answer, I would lke to state why it is different and why that answer did not help me to solve my problem:

I am not asking about how to access the parameters of a parent class from a child class, but if it was possible to instantiate a class with a parent class inherited, passing to it the arguments like if it was a call to the parent's class.

The answer is "I can't", unless I define again the same arguments in the child class __init__() method. Finally, as I said I am new to OOP, the indicated answer was not helpful, as the initial question was too difficult to understand to me (and it's not even in python).

Community
  • 1
  • 1
umbe1987
  • 2,894
  • 6
  • 35
  • 63
  • 1
    You can't. You have to specify the parameters that a function takes. `B.__init()__` has to have the call signature that you call it with. – Morgan Thrapp Nov 04 '16 at 14:11
  • ok, so I have to do something like classB(A): def __init__(self,x,y) super().__init__(x,y) if I need to do this, or rewrite the parent class. – umbe1987 Nov 04 '16 at 14:14
  • @Morgan Thrapp That is not quite the same. And please also stop downvoting everything as I stated I am new to OOP, and the answer of Vitalii Strimbanu was correct (although I'd probably do something else) – umbe1987 Nov 04 '16 at 14:20

1 Answers1

0
class A(): # parent class
     def __init__(self, x, y):
         self.x = x
         self.y = y

class B(A): # child class
    def __init__(self, x, y):
        A.__init__(self, x, y)

So now you can use b = B(5, 5)

  • 1
    Don't explicitly reference the parent class, use `super()`. – Morgan Thrapp Nov 04 '16 at 14:15
  • I like to use explicitly, unless you have a reasonable argument not to do so, please revise your comments. – Vitalii Strimbanu Nov 04 '16 at 14:17
  • 3
    Using the parent explicitly should be the exception rather than the rule. What if that parent changes? Or if you have multiple levels of inheritance? This just makes your code less flexible and less idiomatic. – Morgan Thrapp Nov 04 '16 at 14:20
  • 5
    Keep in mind that if you are not going to do anything else in that `__init__`, there is no need to declare it (the default behavior is using parent init if you don't have one) – campos.ddc Nov 04 '16 at 14:26
  • @campos.ddc This is what I needed, thanks! For now I would stay with something like class B(A): pass, adding functionalities to B as I need them (in my specific case, A would be a generic character class and B (e.g.) the hero of my video game, C an enemy, and so on...) – umbe1987 Nov 04 '16 at 14:40