作者left (881 forever)
看板Python
标题[问题] str.format()
时间Fri Nov 13 09:43:55 2015
问个白痴问题
这是书本上的范例
在下面Point这个class里,有定义一个 __repr__()
如下:
def __repr__(self):
return "Point({0.x!r}, {0.y!r})".format(self)
请问里面的!r是啥意思阿?
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def distance_from_origin(self):
return math.hypot(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __repr__(self):
return "Point({0.x!r}, {0.y!r})".format(self)
def __str__(self):
return "({0.x!r}, {0.y!r})".format(self)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 203.67.77.232
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1447379039.A.8BA.html
1F:→ walelile: repr(0.x) repr(0.y) 11/13 09:58
3F:→ buganini: google://python format "!r" 11/14 16:02