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
cc7756789
V2EX  ›  Python

collections.Counter 中的一段代码不知其意

  •  
  •   cc7756789 · 2015-07-23 20:03:43 +08:00 · 2231 次点击
    这是一个创建于 3207 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def __repr__(self):
            if not self:
                return '%s()' % self.__class__.__name__
            items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
            return '%s({%s})' % (self.__class__.__name__, items)
    
    >>> c = Counter("saasaaaaa")
    >>> c
    Counter({'a': 7, 's': 2})
    

    operator.__mod__(4, 3) 是取模,但是这个 '%r: %r'.mod 是什么玩意呢?
    '%r: %r'.__mod__(4, 3) 等操作都报错,str.__mod__ 是个 <slot wrapper '__mod__' of 'str' objects> 但是我不知道去哪里找源码,官方文档上好像也没有介绍。

    3 条回复    2015-07-24 14:13:33 +08:00
    zhyu
        1
    zhyu  
       2015-07-23 20:37:27 +08:00
    str 的 mod 是格式化
    wodesuck
        2
    wodesuck  
       2015-07-23 21:07:24 +08:00 via Android
    '%r: %r' % (xxx,xxx)
    mulog
        3
    mulog  
       2015-07-24 14:13:33 +08:00
    string formatting
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2430 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:12 · PVG 17:12 · LAX 02:12 · JFK 05:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.