Python Forum
Child class inheritance issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Child class inheritance issue
#1
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?
buran write Apr-21-2022, 10:43 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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()
Output:
Ramesh Ram Just inside class C Calling Class A constructor name me
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Apr-21-2022, 10:28 AM)menator01 Wrote: 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()
Output:
Ramesh Ram Just inside class C Calling Class A constructor name me


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.
Reply
#4
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/9575...-right-way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 759 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
Question __init__ of Child Class zero_fX0 4 1,733 Mar-22-2023, 05:23 PM
Last Post: deanhystad
  Using one child class method in another child class garynewport 5 1,609 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Importing issues with base class for inheritance riccardoob 5 4,720 May-19-2021, 05:18 PM
Last Post: snippsat
  3D vector class with inheritance from 2D vector class buss0140 4 3,160 Dec-20-2020, 08:44 PM
Last Post: deanhystad
  Class inheritance oclmedyb 3 2,272 Dec-09-2020, 04:43 PM
Last Post: deanhystad
  Can we access instance variable of parent class in child class using inheritance akdube 3 14,006 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Issue referencing new instance from other class nanok66 3 2,239 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Performance degradation with IO class inheritance wsygzyx 2 2,133 Jun-18-2020, 06:02 PM
Last Post: wsygzyx
  calling on a method from one class into another class which is not a child NABA 5 2,828 Apr-29-2020, 07:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020