推荐学习书目
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
tunetoystory
V2EX  ›  Python

问一个python的问题嘛

  •  
  •   tunetoystory · Sep 3, 2012 · 3421 views
    This topic created in 4999 days ago, the information mentioned may be changed or developed.
    我有一个对象 a 它有一个属性 b 值为 c

    那么我 a.b 这样就能得到 c

    现在我有个变量 d = 'b'

    我怎么才能通过变量 d 拿到对象 a 中的 b 的值

    举个例子,如果在 php 中能这样 $a->$d
    5 replies    1970-01-01 08:00:00 +08:00
    raptium
        1
    raptium  
       Sep 3, 2012   ❤️ 1
    a.__dict__[d]
    a.__getattr__(d)
    或者 a.__getattribute__(d) 也許更好
    yuelang85
        2
    yuelang85  
       Sep 3, 2012   ❤️ 1
    getattr(a, d)
    tunetoystory
        3
    tunetoystory  
    OP
       Sep 3, 2012
    @raptium
    @yuelang85
    谢谢你们! ok了!
    013231
        4
    013231  
       Sep 3, 2012
    你也可以實現類的__getitem__, __setitem__方法, 然後就可以像Javascript那樣用'[]'運算符訪問屬性了.
    tunetoystory
        5
    tunetoystory  
    OP
       Sep 3, 2012
    @013231 嗯,这个比较高端
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3229 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 12:51 · PVG 20:51 · LAX 05:51 · JFK 08:51
    ♥ Do have faith in what you're doing.