+ 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()

10th Jun 2017, 3:37 AM
Shriram
Shriram - avatar
5 odpowiedzi
+ 12
What would your output look like? Maybe try this (just a quick guess): Bond = Child("Shriram") Bond.last_name() Bond.first_name()
10th Jun 2017, 4:02 AM
Jafca
Jafca - avatar
+ 12
So is my answer what you wanted?
10th Jun 2017, 4:07 AM
Jafca
Jafca - avatar
+ 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.
10th Jun 2017, 5:41 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
it prints Varadarajan Shriram
10th Jun 2017, 4:06 AM
Shriram
Shriram - avatar
0
no I'm sorry but when I try to call the function which is a part of the object it gives an error
10th Jun 2017, 4:16 AM
Shriram
Shriram - avatar