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

有没有办法改变 str 的 __repr__

  •  
  •   tioover ·
    tioover · 2014-03-19 16:04:48 +08:00 · 3148 次点击
    这是一个创建于 3684 天前的主题,其中的信息可能已经有所发展或是发生改变。
    默认输出单引号,但需要双引号……

    我猜要用 metaclass
    5 条回复    1970-01-01 08:00:00 +08:00
    mengzhuo
        1
    mengzhuo  
       2014-03-19 16:33:27 +08:00
    直接replace不能用?

    我猜楼主是要输出json?格式化数据就统统交给各个库就好了
    Shared
        2
    Shared  
       2014-03-19 17:25:44 +08:00
    你不能直接覆盖 built-in types,如果要修改 str 的行为有个变通的方法

    http://gist.github.com/anonymous/9638324

    不知道能不能满足你的需要
    timonwong
        3
    timonwong  
       2014-03-19 18:32:03 +08:00
    Check the following python script if you:
    1) are going down
    2) are using CPython

    https://github.com/gabrielfalcao/sure/blob/master/sure/magic.py
    tioover
        4
    tioover  
    OP
       2014-03-19 18:37:23 +08:00 via Android
    @mengzhuo 我就是在写 parser
    tioover
        5
    tioover  
    OP
       2014-03-19 18:37:44 +08:00 via Android
    @timonwong THX 看看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2864 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:44 · PVG 14:44 · LAX 23:44 · JFK 02:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.