def __str__(self):
return str(self.convert2metres())
def __repr__(self):
return "Length(" + str(self.value) +", '" + self.unit + "')"
if __name__ == "__main__":
x = Length(4)
print(x)
y = eval(repr(x))
z = Length(4.5, "yd") + Length(1)
print(repr(z))
print(z)
x += Length(5)
print(x)
output
4
Length(5.593613298337708, 'yd')
5.1148
9.0
Verbesserungsmöglichkeit:
z = Length(4.5, "yd") + 1