Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
__init__ of Child Class
#1
Question 
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 :
Error:
NameError Traceback (most recent call last) <ipython-input-80-3762dc95e937> in <module> ----> 1 childobject=Child() <ipython-input-79-b57528e5fe56> in __init__(self) 2 3 def __init__(self): ----> 4 super().__init__(self,a,b) 5 print("From Child Class") NameError: name 'a' is not defined
Reply
#2
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")
zero_fX0 likes this post
Reply
#3
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 pass
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?


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 .
Reply
#4
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 ?
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Initiating an attribute in a class __init__: question billykid999 8 1,309 May-02-2023, 09:09 PM
Last Post: billykid999
  Using one child class method in another child class garynewport 5 1,555 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Child class inheritance issue eakanathan 3 1,325 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,313 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,767 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,955 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  calling on a method from one class into another class which is not a child NABA 5 2,806 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  XML Parsing Child karthi_python 1 1,878 May-16-2019, 01:37 PM
Last Post: karthi_python
  A problem with child class Truman 2 2,779 Jul-02-2018, 12:37 AM
Last Post: ichabod801
  parent/add and child/div PyMan 1 2,405 Feb-23-2018, 04:38 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020