class 之 ___repr__

来源:互联网 发布:mac osx系统下载 编辑:程序博客网 时间:2024/05/21 07:13

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
0 0
原创粉丝点击