![]() |
__init__ of Child Class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: __init__ of Child Class (/thread-39652.html) |
__init__ of Child Class - zero_fX0 - Mar-22-2023 Hello all, Base is parent and Child inherited . Should argument(s) in the __init__of the child class be same as what we have in the __init__ 's parent? I get an error. class Base(): def __init__(self,a,b): self.a = a self.b = b print("From Parent Class") class Child(Base): def __init__(self): super().__init__(self,a,b) # or Base.__init__(self,a,b) print("From Child Class") childobject=Child()After trying to make an object from Child Class :
RE: __init__ of Child Class - deanhystad - Mar-22-2023 In Base.__init__(), a and b are positional arguments, meaning you are required to pass values for a and b. You try to do that, but you don't have any values to pass. As far as Child.__init__() is concerned, a and b are unassigned local variables. They have no value. If a and b are important attributes in Base, You should pass values for these to Child.__init__(). That way you can properly initialize Base using the values you pass to Child(a_value, b_value). class Child(Base): def __init__(self, a. b): super().__init__(a, b) # Notice that self should not be included as an arg. print("From Child Class")Another optiion is to pass default values for a and b. class Child(Base): def __init__(self): super().__init__(0, 0) # Whatever values make sense as defaults for a and b print("From Child Class") RE: __init__ of Child Class - zero_fX0 - Mar-22-2023 Thanks for your response. (Mar-22-2023, 03:01 PM)deanhystad Wrote: In Base.__init__(), a and b are positional arguments, meaning you are required to pass values for a and b. You try to do that, but you don't have any values to passYou mean I should create an object for Parent firstly which make me able to pass values?But how passing variables through a Parent class can help for second Child Class? Here: super().__init__(a, b) # Notice that self should not be included as an arg.Why self keyword should not be included there? In many examples I've seen it. Your explanation is excellent but I m getting confused why still needs to be passed a , b in Child.__init__ ( ) . It is already passed in parent, why we have to concern it for Child Class ? If we want to pass a , b in Constructor of Child Class , why we create a inheritance class from Base class? To be honest, I still have problem of this part of code: def __init__(self, a, b):Thanks in-advance If you provide more details . RE: __init__ of Child Class - zero_fX0 - Mar-22-2023 If I pass a , b in Child.__init__( ) as you mentioned : For creating an object from Child class like CHILD_OBJECT=Child() also should pass two variables(a and b) but in action I won't want that.Parent class should concern for those two not Child class . How should solve this conflict ? RE: __init__ of Child Class - deanhystad - Mar-22-2023 Quote:Why self keyword should not be included there? In many examples I've seen it.You have not seen it. Calling an instance method using instance.method(args) gets automatically translated to instance.__class__.method(instance, args). If you call super().__init__(self, a, b) this is automatically translated to Base.__init__(self, self, a, b). What you saw in examples was probably this: class Child(Base): def __init__(self): Base.__init__(self, a, b) print("From Child Class")This is not using an instance to call Base.__init__(), so the translation does not occur. Quote:You mean I should create an object for Parent firstly which make me able to pass values?But how passing variables through a Parent class can help for second Child Class?No. I am saying that you have to pass values for a and b when you call Base.__init__(). Child.__init__() has to do this: super().__init__(a, b)You have to decide where a and b come from. If the values are important, I pass them as arguments to Child. class Child(Base): def __init__(sel, a, b): Base.__init__(self, a, b) print("From Child Class")If I don't want to always provide values, I might make a and b named arguments and give them default values. Now I can provide a, b values when creating Child(4, "Goodbye"), or I can use the default values Child(). class Child(Base): def __init__(self, a=5, b="Hello"): Base.__init__(self, a, b) print("From Child Class")Or you can hide a and b inside Child and use default values when calling Base.__init__(). class Child(Base): def __init__(self): Base.__init__(self, 5, "Hello") print("From Child Class")To change the value for Base.a or Base.b I would either set the attributes or call instance methods after the Child object is created. |