'Item({!r})'.format(self.name) 中的 !r 是什么意思

2019-03-03 13:20:59 +08:00
 patrickstar

Python Cookbook 3rd 的 1.5 章有一段代码如下:

>>> class Item:
...     def __init__(self,name):
...             self.name=name
...     def __repr__(self):
...             return 'Item({!r})'.format(self.name)
...

请问 return 语句中的 !r 是什么意思,我咋个都搜不到,谢谢!

3128 次点击
所在节点    Python
2 条回复
wd
2019-03-03 13:25:28 +08:00
Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().
patrickstar
2019-03-03 13:26:14 +08:00
@wd thanks

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/540530

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX