+ 1
Why is this code giving me an error? I just can't call the method last_name1 from class Parent even though Bond (Family object)
class Parent: def __init__(self,x): self.lastname = x def last_name1(self): print(self.lastname) class Child: def __init__(self,x): self.firstname = x def first_name(self): print(self.firstname) def last_name(self): #overwrites fuction in parent print('Varadarajan') class Family(Child, Parent): pass Bond = Family("Shriram") Bond.last_name1() Bond.first_name()
5 ответов
+ 12
What would your output look like?
Maybe try this (just a quick guess):
Bond = Child("Shriram")
Bond.last_name()
Bond.first_name()
+ 12
So is my answer what you wanted?
+ 4
That won't work. You need to reimplement the inheritance scheme and make Child inherit from Parent and Family inherit from Child. Also, make sure you know what you are doing in the __init__ method, since it will get *overwritten* and not *appended*.
Please review the "Inheritance" section of the Python course here - "Object-Oriented Programming" part.
+ 1
it prints
Varadarajan
Shriram
0
no I'm sorry but when I try to call the function which is a part of the object it gives an error