【问题标题】:How can I use variables in statements with Python?如何在 Python 语句中使用变量?
【发布时间】:2013-02-26 04:34:38
【问题描述】:
sname = "example"
some_class.<sname>.do_stuff()

snamesome_class 中子类的名称。是否可以在我的调用中动态引用字符串,从而调用some_class.example.do_stuff()

【问题讨论】:

    标签: python string class


    【解决方案1】:

    您正在寻找 getattr() 内置函数:

    getattr(some_class, sname).do_stuff()
    

    【讨论】:

      猜你喜欢
      • 2022-12-21
      • 2010-10-28
      相关资源
      最近更新 更多