Answers for "python multiple inheritance diamond problem"

0

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"
Posted by: Guest on September-25-2020

Code answers related to "python multiple inheritance diamond problem"

Python Answers by Framework

Browse Popular Code Answers by Language