![](/img/trans.png)
[英]How to access class attribute using variable value in Flask python?
[英]Python: access class attribute by variable
在python中如何以cls.variable格式訪問實例的屬性?
(在ruby中,可以采用cls。#{variable}格式)
>>>cls.classname
u'CIM_RegisteredProfile'
>>>var='classname'
>>>cls.var
Traceback (most recent call last):
File "<console>", line 0, in <module>
AttributeError: 'CIMClass' object has no attribute 'var'
使用getattr
:
var = 'classname'
getattr(cls, var)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.