V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Mar 19, 2014 · 3833 views
    This topic created in 4434 days ago, the information mentioned may be changed or developed.
    默认输出单引号,但需要双引号……

    我猜要用 metaclass
    5 replies    1970-01-01 08:00:00 +08:00
    mengzhuo
        1
    mengzhuo  
       Mar 19, 2014
    直接replace不能用?

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

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

    不知道能不能满足你的需要
    timonwong
        3
    timonwong  
       Mar 19, 2014
    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
       Mar 19, 2014 via Android
    @mengzhuo 我就是在写 parser
    tioover
        5
    tioover  
    OP
       Mar 19, 2014 via Android
    @timonwong THX 看看
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2787 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 01:41 · PVG 09:41 · LAX 18:41 · JFK 21:41
    ♥ Do have faith in what you're doing.