-1
大厦在这个问题上 looping over all member variables of a class in python循环执行类变量的值在Python
关于如何遍历一个类的变量。我想遍历类变量值并存储在一个列表中。
class Baz:
a = 'foo'
b = 'bar'
c = 'foobar'
d = 'fubar'
e = 'fubaz'
def __init__(self):
members = [attr for attr in dir(self) if not attr.startswith("__")]
print members
baz = Baz()
将返回['a', 'b', 'c', 'd', 'e']
我想在列表中的类属性值。
使用'的理解内部getattr'。 – 2014-08-27 19:10:43