float sub-class 改变摄入量和 __str__ 行为
float sub-class to alter intake and __str__ behaviour
我已经将 float 子类化以改变它的 __str__()
方法以一个符号结尾(在我的例子中是 €)。
输入被过滤以删除一个符号(在我的例子中是 €)。
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} €'
但是当我打印时,出现递归打印错误。
g = Euro('123.23 €')
print (g)
错误:
Euro.py, line __str__
return f'{self} €'
[Previous line repeated 329 more times]
RecursionError: maximum recursion depth exceeded
使用super()
调用父类的方法,避免递归错误
def __str__(self):
return super().__str__() + ' €'
>>> g = Euro('123.23 €')
>>> print(g)
123.23 €
不要使用继承; Euro
不是 float
的一种,由于实数的浮点近似值不精确,因此永远不应使用 float
表示货币。
相反,使用 组合 来存储表示欧元数量的属性,使用类似 decimal.Decimal
的东西来表示欧元和美分 恰好.
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} €'
我得到了以下代码:
from decimal import Decimal
class Euro:
def __init__(self, value):
if isinstance(value, str):
value = value.strip(' €')
self.value = Decimal(value)
def __add__(self, other):
return Euro(self.value + other.value)
def __sub__(self,other):
return Euro(self.value - other.value)
def __str__(self):
return f'{self.value} €'
我看没有必要分class。
我添加了对 return 欧元对象的 +/- 运算符的支持。
结果是:
g = Euro(1.00)
h = Euro('10.00 €')
print (h+g) # --> 11.00 €
print (h-g) # --> 9.00 €
我已经将 float 子类化以改变它的 __str__()
方法以一个符号结尾(在我的例子中是 €)。
输入被过滤以删除一个符号(在我的例子中是 €)。
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} €'
但是当我打印时,出现递归打印错误。
g = Euro('123.23 €')
print (g)
错误:
Euro.py, line __str__
return f'{self} €'
[Previous line repeated 329 more times]
RecursionError: maximum recursion depth exceeded
使用super()
调用父类的方法,避免递归错误
def __str__(self):
return super().__str__() + ' €'
>>> g = Euro('123.23 €')
>>> print(g)
123.23 €
不要使用继承; Euro
不是 float
的一种,由于实数的浮点近似值不精确,因此永远不应使用 float
表示货币。
相反,使用 组合 来存储表示欧元数量的属性,使用类似 decimal.Decimal
的东西来表示欧元和美分 恰好.
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} €'
我得到了以下代码:
from decimal import Decimal
class Euro:
def __init__(self, value):
if isinstance(value, str):
value = value.strip(' €')
self.value = Decimal(value)
def __add__(self, other):
return Euro(self.value + other.value)
def __sub__(self,other):
return Euro(self.value - other.value)
def __str__(self):
return f'{self.value} €'
我看没有必要分class。 我添加了对 return 欧元对象的 +/- 运算符的支持。
结果是:
g = Euro(1.00)
h = Euro('10.00 €')
print (h+g) # --> 11.00 €
print (h-g) # --> 9.00 €