【发布时间】:2012-11-09 06:58:39
【问题描述】:
来自
a = []
class A(object):
def __init__(self):
self.myinstatt1 = 'one'
self.myinstatt2 = 'two'
到
a =['one','two']
【问题讨论】:
标签: python
来自
a = []
class A(object):
def __init__(self):
self.myinstatt1 = 'one'
self.myinstatt2 = 'two'
到
a =['one','two']
【问题讨论】:
标签: python
Python 有一个方便的内置函数,称为 vars,它会将属性作为 dict 提供给您:
>>> a = A()
>>> vars(a)
{'myinstatt2': 'two', 'myinstatt1': 'one'}
要仅获取属性值,请使用适当的dict 方法:
>>> vars(a).values()
['two', 'one']
在 python 3 中,这会给你一个与列表稍有不同的东西 - 但你可以在那里使用list(vars(a).values())。
【讨论】:
尝试查看__dict__ 属性。它会帮助你:
a = A().__dict__.values()
print a
>>> ['one', 'two']
【讨论】: