Python Forum
Base class variables are not accessible
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Base class variables are not accessible
#1
Hi,

I just want to understand, why I'm getting below error in inheritance?
Error:
Traceback (most recent call last): File "inheritance.py", line 13, in <module> main() File "inheritance.py", line 10, in main print b_obj.aa AttributeError: 'B' object has no attribute 'aa'
This is my python code
class A(object):
    def __init__(self):
        self.aa = None

class B(A):
    def __init__(self):
        self.bb = None
def main():
    b_obj = B()
    print b_obj.aa

if __name__ == '__main__':
    main()
Reply
#2
The init function under A is never called so self.aa does not exist. https://www.python-course.eu/python3_inheritance.php
Reply
#3
Hi Wooee,

Thanks for replying back. I thought __init__() is similar to a constructor in other programming language(c++) and so it will be called once we create a derived class object.
Reply
#4
Python’s super() considered super! by Raymond Hettinger

Based on python print statement I guess you use python2. In this case this should work
class A(object):
    def __init__(self):
        self.aa = None

 
class B(A):
    def __init__(self):
        super(B, self).__init()__
        self.bb = None


def main():
    b_obj = B()
    print b_obj.aa

 
if __name__ == '__main__':
    main()
In python3 (and you should be using it, because python2 official support ends Jan, 1st 2020) same code will look like

class A(object):
    def __init__(self):
        self.aa = None

 
class B(A):
    def __init__(self):
        super().__init()__
        self.bb = None


def main():
    b_obj = B()
    print(b_obj.aa)

 
if __name__ == '__main__':
    main()
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unchangeable variables in a class? Calab 12 1,423 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 985 Jan-27-2023, 05:32 PM
Last Post: caslor
  Calling a base class variable from an inherited class CompleteNewb 3 1,595 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,785 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 10,397 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 1,868 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Importing issues with base class for inheritance riccardoob 5 4,595 May-19-2021, 05:18 PM
Last Post: snippsat
  Behavior of Abstract Base Class dgrunwal 4 2,135 Oct-15-2020, 07:19 PM
Last Post: Gribouillis
  Class variables menator01 2 1,958 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  How to make Python 3.7.6 accessible to users Doguhan 4 2,728 May-19-2020, 05:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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