您的位置:首页 > 其它

class 之 ___repr__

2016-01-26 15:10 204 查看
When defining a new repr(), return a string value that uses the member variables of the class to display the 3D point properly. You can use the str() function to put these numbers in the proper string.

For advanced users: For more information on repr and other special methods see this Python documentation. Note the slight difference between the repr and str methods.

class Point3D (object):
def __init__(self,x,y,z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return "(%d, %d, %d)" %(self.x, self.y, self.z) ##这里自己会经常用错格式,需要注意
my_point = Point3D (1,2,3)
print my_point
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: