首页 > 编程语言 > 详细

python特殊方法定制类

时间:2017-08-13 12:59:31      阅读:205      评论:0      收藏:0      [点我收藏+]
#coding:utf-8
class RoundFloat(object):
def __init__(self,val):
assert isinstance(val, float),"value must be a float"
self.value = round(val,2)
def __str__(self):
return "{:.2f}".format(self.value)

__repr__ = __str__
#def __repr__(self):
# return self.__str__()

r = RoundFloat(3.1567)
print r
print type(r)

output result:

3.16
<class ‘__main__.RoundFloat‘>

python特殊方法定制类

原文:http://www.cnblogs.com/klsw/p/7353063.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!