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
  Return a string or byte object from Enum class? Calab 2 120 Yesterday, 09:43 PM
Last Post: DeaD_EyE
  Python inner classes inheritance from parent class Abedin 8 1,089 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 1,115 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,426 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,539 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,084 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 4,055 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 2,343 Jul-12-2024, 09:36 AM
Last Post: CAD79
  [split] Class and methods ebn852_pan 15 3,569 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,370 May-13-2024, 05:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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