python - 创建 float 的子类,以防所有算术运算都返回同一类的对象(不是 float)
问题描述
我的课堂示例:
class Ff(float):
def __str__(self):
return '{:.2f}'.format(self) + '$'
a = Ff(1)
print(type(a))
print(a)
返回:
<class '__main__.Ff'>
1.00$
但万一:
a = Ff(1) + 2
print(type(a))
print(a)
返回:
<class 'float'>
3.0
如何在结果中添加Ff(1) + 2
或为 radd2 + Ff(1)
获得相同的 <class ' main .Ff'> ?
解决方案
推荐阅读
- angularjs - 使用 angularjs 和 mqql 将数据拉取到模态
- splunk - 在 splunk 中计数和求和
- reactjs - ajax 发布请求 contentType 导致“错误请求”错误
- .net-core - 使用 SharePoint REST POST API:从 HttpWebRequest 获取 400 错误
- postgresql - 如何处理具有排除约束的表的更新?
- jdbc - 如何在不使用表或模式模式的情况下在 JDBC 中查找雪花表列
- javascript - 如何修复 travis 上的“未捕获的错误:spawn ... EACCES”
- java - 单击提交按钮时未找到 404 - Servlet 不工作
- postgresql - postgresql 副本中的附加索引
- react-native - Vue 原生抽屉导航