-3
class A:
    def __init__(self):
        self.name = "John"
        self.age = 27
        self.height = 5.3
    
    def display(self):
        print("Name is", self.name)
        print("Age is", self.age)
        print("Height is", self.height)
class B(A):
    def __init__(self):
        self.name = "Epril"
        self.age = 0
        # self.height = 0
    
    def display(self):
        print("Name is", self.name)
        print("Age is", self.age)
        print("Height is", self.height)
b = B()
b.display()

Output:

Name is Epril
Age is 0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/var/folders/dj/63hdc9sn547dw023v9ngwnvm0000gn/T/ipykernel_59756/1543080232.py in <module>
----> 1 b.display()

/var/folders/dj/63hdc9sn547dw023v9ngwnvm0000gn/T/ipykernel_59756/2847412845.py in display(self)
      8         print("Name is", self.name)
      9         print("Age is", self.age)
---> 10         print("Height is", self.height)

AttributeError: 'B' object has no attribute 'height'

1 Answers1

0

you have to the parent class (super) init() in your child class as well :

class B(A):
    def __init__(self):
        super().__init__() #thisline 
        self.name = "Epril"
        self.age = 0
eshirvana
  • 23,227
  • 3
  • 22
  • 38