设为首页 加入收藏

TOP

python特殊方法定制类
2017-09-30 16:59:21 】 浏览:1742
Tags:python 特殊 方法 定制
#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'>

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Tornado源码分析之起步 下一篇[原创] Python3.6+request+beauti..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目