1# Example of multiple inheritance
2# I recommend to avoid it, because it's too complex to be relyed on.
3
4class Thing(object):
5 def func(self):
6 print("Function ran from class Thing()")
7
8class OtherThing(object):
9 def otherfunc(self):
10 print("Function ran from class OtherThing()")
11
12class NewThing(Thing, OtherThing):
13 pass
14
15some_object = NewThing()
16
17some_object.func()
18some_object.otherfunc()
1# example of diamond problem and multiple inheritance
2
3class Value():
4 def __init__(self, value):
5 self.value = value
6 print("value")
7
8 def get_value(self):
9 return self.value
10
11class Measure(Value):
12 def __init__(self, unit, *args, **kwargs):
13 print ("measure")
14 self.unit = unit
15 super().__init__(*args, **kwargs)
16
17 def get_value(self):
18 value = super().get_value()
19 return f"{value} {self.unit}"
20
21class Integer(Value):
22 def __init__(self, *args, **kwargs):
23 print("integer")
24 super().__init__(*args, **kwargs)
25
26 def get_value(self):
27 value = super().get_value()
28 return int(value)
29
30class MeasuredInteger(Measure, Integer):
31 def __init__(self, *args, **kwargs):
32 super().__init__(*args, **kwargs)
33
34
35mt = MetricInteger("km", 7.3)
36# prints:
37# measure
38# integer
39# value
40
41mt.get_value() # returns "7 km"