这
a = []
class A(object):
def __init__(self):
self.myinstatt1 = 'one'
self.myinstatt2 = 'two'
到
a =['one','two']
答案 0 :(得分:9)
Python有一个方便的内置命名为vars,它将属性作为dict提供给你:
>>> a = A()
>>> vars(a)
{'myinstatt2': 'two', 'myinstatt1': 'one'}
要获取属性值,请使用相应的dict
方法:
>>> vars(a).values()
['two', 'one']
在python 3中,这会给你一个与列表略有不同的东西 - 但你可以在那里使用list(vars(a).values())
。
答案 1 :(得分:2)
尝试查看__dict__
属性。它会对你有所帮助:
a = A().__dict__.values()
print a
>>> ['one', 'two']