( Python, 3.0 )
I have defined a class called
Money_value in which I want to inherit the
float class, but whenever I do something that is not defined by the new class, the variable becomes float ?
python
class Money_value(float):
def __str__(self):
"""Return a "beautified" string, to look more like a standard 2-decimal money value
"""
value_list = super().__str__().split(".")
if len(value_list[1]) == 1:
return "$" + value_list[0] + "." + value_list[1] + "0"
else:
cent_list = [x for x in value_list[1]]
# round the second decimal if needed
if len(cent_list) > 2 and int(cent_list[2]) >= 5:
cent_list[1] = str(int(cent_list[1]) + 1)
return "$" + value_list[0] + "." + "".join(cent_list[:2])
so lets say I want to do this:
python
>>> value = Money_value(83.45)
>>> value += 2.30
>>> type(value)
<class 'Money_value'>
I get this:
python
>>> value = Money_value(83.45)
>>> value += 2.30
>>> type(value)
<class 'float'>
I can even do:
python
>>> value = Money_value(83.45)
>>> value += Money_value(2.30)
>>> type(value)
<class 'float'>
I'm so confused as to why any variable continues to silently becomes float?