-1

I am not able to call the variables and constructor of parent class using inheritance.

class base():
    #age =24;
    def __init__(self,age):
        self.age = age;
        print("i am in base")

class Date(base):
    def __init__(self,year,month,date):
        self.year = year
        self.month = month
        self.date = date

class time(Date):
    def greet(self):
        print('i am in time')

a = time(1,2,4)
print(a.year)
print(a.age) #this line is throwing error...

Please help, how to call the constructor of the parent class

0

1 Answer 1

1

What you are doing in your example is not multiple inheritance. Multiple inheritance is when the same class inherits from more than one base class.

The problem you are having is because none of your child classes call the parent constructor, you need to do that and pass the required parameters:

class Date(base):
    def __init__(self, age, year, month, date):
        super().__init__(age)
        self.year = year
        self.month = month
        self.date = date

class time(Date):
    def __init__(self, age, year, month, date):
         super().__init__(age, year, month, date)

    def greet(self):
        print('i am in time')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.