如何获取一个可迭代的类中所有变量的列表?就像locals()一样,但是针对类
class Example(object):
bool143 = True
bool2 = True
blah = False
foo = True
foobar2000 = False
def as_list(self)
ret = []
for field in XXX:
if getattr(self, field):
ret.append(field)
return ",".join(ret)
这应该会返回>>> e = Example()
>>> e.as_list()
bool143, bool2, foo
for field in [ self.bool143, self.bool2, self.blah, self.foo, self.foobar2000 ]
呢?怎么会不知道类的实例变量呢? - S.Lott