H Doucet :
I've sub-classed float to alter its __str__()
method to end with a symbol (in my case €).
The input is filtered to remove a symbol (in my case €).
class Euro(float):
def __new__(cls, value):
v = ''.join([_ for _ in value if _ != '€' ])
return super(Euro, cls).__new__(cls, v)
def __str__(self):
return f'{self} €'
But when I print, I get a recursive print error.
g = Euro('123.23 €')
print (g)
Error:
Euro.py, line __str__
return f'{self} €'
[Previous line repeated 329 more times]
RecursionError: maximum recursion depth exceeded
chepner :
Don't use inheritance; a Euro
is not a kind of float
, and money should never be represented using float
s due to the imprecision of the floating-point approximations of real numbers.
Instead, use composition to store an attribute representing the number of Euros, using something like decimal.Decimal
to represent the euros-and-cents exactly.
from decimal import Decimal
class Euro:
# In accordance with how to use super properly,
# always accept and pass on unrecognized keyword arguments.
def __init__(self, value, **kwargs):
super().__init__(**kwargs)
self.value = Decimal(value.strip('€'))
def __str__(self):
return f'{self.value} €'