![]() |
Child class inheritance issue - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Child class inheritance issue (/thread-36995.html) |
Child class inheritance issue - eakanathan - Apr-21-2022 class A: def __init__(self, name, age): self.name = name self.age = age def details(self): print (self.name) obj = A('Ramesh', 62) obj.details() class B: def __init__(self, name, id): self.name = name self.id = id def details1(self): print (self.name) obj = B('Ram', 1234) obj.details1() class C(A,B): print("Just inside class C") def __init__(self): A.__init__(self) # calling class A constructor print('Calling Class A constructor') def get_details(self): A.details(self) print(A.details(self.name))Class A and B output are fine. When I try with class C, it prints "just inside class C", after that no output, no error message. Where I am missing? Can any one help pl? RE: Child class inheritance issue - menator01 - Apr-21-2022 Please use tags when posting code. Maybe something like this class A: def __init__(self, name, age): self.name = name self.age = age def details(self): print (self.name) obj = A('Ramesh', 62) obj.details() class B: def __init__(self, name, id): self.name = name self.id = id def details1(self): print (self.name) obj = B('Ram', 1234) obj.details1() class C(A,B): print("Just inside class C") def __init__(self): A.__init__(self, 'name me', 23) # calling class A constructor print('Calling Class A constructor') def get_details(self): A.details(self) print(A.details(A.self.name)) obj = C() obj.details()
RE: Child class inheritance issue ----"menator01" pid='156269' - eakanathan - Apr-21-2022 (Apr-21-2022, 10:28 AM)menator01 Wrote: Please use tags when posting code. Thanks a ton for immediate response to my very basic in python. Pardon me for asking such questions, but wish to learn on my own. Real pleasure and elated to get immediate response. Thank you once again. Note: will use tags in future. RE: Child class inheritance issue - deanhystad - Apr-21-2022 This code executes when the class C code is parsed, not when an instance of the class is made. print("Just inside class C")I know that classes A, B and C are all just examples, but they are horrible examples. If C inherits from classes A and B, it would be because both A and B have things to offer that are different from each other and C should call the __init__() methods for both A and B. This is a pretty good, short article about multiple inheritance in Python. https://stackoverflow.com/questions/9575409/calling-parent-class-init-with-multiple-inheritance-whats-the-right-way |