简体   繁体   中英

Python: access class attribute by variable

in python how to access instance's attribute as cls.variable format?

(in ruby, it can be in format 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'

Use getattr :

var = 'classname'
getattr(cls, var)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM