V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
patrickstar
V2EX  ›  Python

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

  •  1
     
  •   patrickstar · 2019-03-03 13:20:59 +08:00 · 3120 次点击
    这是一个创建于 1879 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 是什么意思,我咋个都搜不到,谢谢!

    2 条回复    2019-03-03 13:26:14 +08:00
    wd
        1
    wd  
       2019-03-03 13:25:28 +08:00 via iPhone   ❤️ 1
    Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().
    patrickstar
        2
    patrickstar  
    OP
       2019-03-03 13:26:14 +08:00
    @wd thanks
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1097 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:54 · PVG 02:54 · LAX 11:54 · JFK 14:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.