Search code examples
pythonpython-3.xinheritancemultiple-inheritance

Is there a good way to make a class inherit from one of two classes


I want to know if there is a good way to make a class inherit from either of two options. Let's say I have a Employee, Devloper and Manager class. Developer inherits from Employee, pretty straight forward, the Manager should inherit from Employee, or Developer. So in essence always inherit from Empolyee and possible also from Developer. Is there a way to do this? Or am I complicating it somehow and shouldn't use inheritance in the first place.

I got it to work in my own dirt method but it feels like a hacky workaround. Maybe this is the right way to do this but I should somehow restrict useage.

This is my manager class with the hacky workaround, I am basically checking the length of the passed arguments and deciding if they belong to a Developer of Employee:

class Manager(Developer or Employee):

    def __init__(self, *args, **kwargs):
        print(args, kwargs)
        if len(args) == 2:
            Employee.__init__(self, *args, **kwargs)
        elif len(args) == 3:
            Developer.__init__(self, *args, **kwargs)
        else:
            print('error')

My Employee and Developer classes being these:

class Employee:

    def __init__(self, first, last):
        self.first = first
        self.last = last

    def fullname(self):
        return '{} {}'.format(self.first, self.last)

class Developer(Employee):

    def __init__(self, first, last, language):
        super().__init__(first, last) #or Employee.__init__()
        self.language = language

    #Why can i not do something like:
    #def __init__(self, language):
    #    self.language = language
    #And because the class extends Employee it would also need it's arguments, them being in order from lowest on inheritance ladder to highest.

    def getlang(self):
        return self.language

I also commented another smaller question here, I felt like if I get restricted to using just a single "layer" of classes I can inherit. Why would I need to pass all arguments to the __init__() and why can python not just figure out what others it would need for the subclasses and pass them along "under the hood".

Please let me know if I am doing any other ill-advised things. I just started out in python!


Solution

  • When I have a problem like this, I use a factory function:

    class Employee:
          .....
    
    class Developer(Employee):
          ...... 
    
    class DeveloperManager(Developer):
       def __int__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
    
    class EmployeeManager(Employee):
       def __int__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
    
    # Factory to create a manager class based on the args given
    def Manager(*args, **kwargs):
        # if language argument is passed return a developer manager
        if len(args) == 3 or len(args) == 2 and 'language' in kwargs:
            return DeveloperManager(*args, **kwargs)
        elif len(args) == 2:
            return EmployeeManager(*args, **kwargs)
        else:
            print('error')