diamond inheritance python code example

Example 1: python multiple inheritance diamond problem

# example of diamond problem and multiple inheritance

class Value():                                                               
    def __init__(self, value):
        self.value = value
        print("value")

    def get_value(self):
        return self.value
        
class Measure(Value):                                                               
    def __init__(self, unit, *args, **kwargs):
        print ("measure")
        self.unit = unit
        super().__init__(*args, **kwargs)
        
    def get_value(self):
        value = super().get_value()
        return f"{value} {self.unit}"

class Integer(Value):
    def __init__(self, *args, **kwargs):
        print("integer")
        super().__init__(*args, **kwargs)
        
    def get_value(self):
        value = super().get_value()
        return int(value)

class MeasuredInteger(Measure, Integer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        
mt = MetricInteger("km", 7.3)
# prints:
# measure
# integer
# value

mt.get_value() # returns "7 km"

Example 2: python multiclass inheritance with inputs

class a:
    def __init__(self):
        print("Hello")

class c:
    def __init__(self, text):
        print(text)
        
class d(a,c):
   def__init__(self,text):
       a.__init__(self)
       c.__init__(self,text)