Search code examples
pythonoopinheritancemultiple-inheritance

Python Multiple Inheritance using Classes with non matching variables


How can I set up multiple inheritance in python using class constructors with arguments? I've seen many examples with no arg constructors but that's no fun... I've done this setup in c++. I am learning python and figured I would try to recreate the same setup.

How can I get my Student_Worker constructor to work without all the *args,*kwargs bs?

If override works from Person to Student and to Worker why doesn't it work from Student and Worker to Student_Worker?

If the diamond case works on classes with no arguments in any of 4 constructors why wouldn't it work in a diamond case that has arguments in the constructors?

Am I just missing something simple? I can't find a decent guide anywhere.

class Person:        #BASE CLASS
    num_of_persons = 0;

    def __init__(self,x,y,z):     
        Person.num_of_persons += 1   
        print("Greetings!")
        self.Name = x
        self.Sex = y
        self.DOB = z
        print("I am " + self.Name)    

    def get_info(self):       
        return '{} {} {}'.format(self.Name,self.Sex,self.DOB)


class Student(Person): #DERIVED CLASS
    def __init__(self, x, y, z, s):
        self.school = s
        return super().__init__(x, y, z)

    def get_info(self):
        return super().get_info() + ' {}'.format(self.school)


class Worker(Person):  #DERIVED CLASS
    def __init__(self, x, y, z, c):
        self.company = c
        return super().__init__(x, y, z)

    def get_info(self):
        return super().get_info() + ' {}'.format(self.company)



class Student_Worker(Student, Worker): #MULTIPLE DERIVED CLASS
    def __init__(self,x,y,z,s,c):
        Student.__init__(x,y,z,c)
        Worker.__init__(c)


p1 = Person("John","M", "1900")
s1 = Student("Sam","F","1910","iSchool")
w1 = Worker("Larry","M","1920","E-SITE")
sw1 = Student_Worker("Brandy","F","1930","iSchool","E-Site")

print(p1.get_info())
print(s1.get_info())
print(w1.get_info())
print(sw1.get_info())

Solution

  • You can try creating the constructors as following:

    1) Student

    class Student(Person): #DERIVED CLASS
        def __init__(self, x, y, z, s):
            Person.__init__(self,x, y, z)
            self.school = s
    

    2) Worker

    class Worker(Person):  #DERIVED CLASS
        def __init__(self, x, y, z, c):
            Person.__init__(self,x, y, z)
            self.company = c
    

    3) Student Worker

    class Student_Worker(Student, Worker): #MULTIPLE DERIVED CLASS
        def __init__(self,x,y,z,s,c):
            Student.__init__(self,x,y,z,s)
            Worker.__init__(self,x,y,z,c)
    

    If you run your code, you will get this output:

    Greetings!
    I am John
    Greetings!
    I am Sam
    Greetings!
    I am Larry
    Greetings!
    I am Brandy
    Greetings!
    I am Brandy
    John M 1900
    Sam F 1910 iSchool
    Larry M 1920 E-SITE
    Brandy F 1930 E-Site iSchool