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
  class Blockage not projecting Azdaghost 1 114 1 minute ago
Last Post: deanhystad
  Return a string or byte object from Enum class? Calab 5 301 Yesterday, 05:21 PM
Last Post: snippsat
  Python inner classes inheritance from parent class Abedin 8 1,111 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 1,134 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,433 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,545 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,091 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 4,084 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 2,354 Jul-12-2024, 09:36 AM
Last Post: CAD79
  [split] Class and methods ebn852_pan 15 3,577 May-23-2024, 11:57 PM
Last Post: ebn852_pan

Forum Jump:

User Panel Messages

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