Python Forum
Can we access instance variable of parent class in child class using inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can we access instance variable of parent class in child class using inheritance
#1
I am pasting my code below:-

class A:
    def __init__(self,i=100): 
      self.i=100
   

class B(A): 
   def __init__(self,j=0): 
      self.j=j 


b=B()
print(b.j)
print(b.i)
I have 2 class A and B . B is inheriting class A so according to the concept of inheritance base class acquires property of parent class so Instance variable i must be accessible to class B . But it is giving me an error that :
Error:
Traceback (most recent call last): File "D:/PYTHON/jljl.py", line 16, in <module> print(b.i) AttributeError: 'B' object has no attribute 'i'
Why this is giving error.
Larz60+ write Nov-12-2020, 01:42 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
you forgot to initialize the instance of A in B
class A:
    def __init__(self,i=100): 
      self.i=100
    
 
class B(A): 
   def __init__(self,j=0):
       A.__init__(self)
       self.j=j 


b=B()
print(b.j)
print(b.i)
Reply
#3
Not that relevant when your class inherits from single class, but in any case better to use super()

class A:
    def __init__(self, i=100): 
        self.i=100
    
 
class B(A): 
   def __init__(self, j=0):
        super().__init__()
        self.j=j 
 
 
b=B()
print(b.j)
print(b.i)
some external resources
https://realpython.com/python-super/
https://stackoverflow.com/questions/2228...-in-python
Larz60+ likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
You need to call __init__ from A in B:
class B(A): 
   def __init__(self, i, j=0): 
      A.__init__(self, i)
      self.j=j 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 378 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 257 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 295 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 369 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 534 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 668 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 739 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 485 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,839 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 615 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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