推荐学习书目
› 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 · 3772 views
    This topic created in 5174 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
    raptium
    Sep 3, 2012❤️1
    1
    a.__dict__[d]
    a.__getattr__(d)
    或者 a.__getattribute__(d) 也許更好
    yuelang85
    yuelang85
    Sep 3, 2012❤️1
    2
    getattr(a, d)
    tunetoystory
    tunetoystory
    OP
    Sep 3, 2012
    3
    @raptium
    @yuelang85
    谢谢你们! ok了!
    013231
    013231
    Sep 3, 2012
    4
    你也可以實現類的__getitem__, __setitem__方法, 然後就可以像Javascript那樣用'[]'運算符訪問屬性了.
    tunetoystory
    tunetoystory
    OP
    Sep 3, 2012
    5
    @013231 嗯,这个比较高端
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1471 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:11 · PVG 03:11 · LAX 11:11 · JFK 14:11
    ♥ Do have faith in what you're doing.