คุณจะได้รับรายการตัวแปรทั้งหมดในคลาสที่สามารถทำซ้ำได้อย่างไร เหมือนคนในท้องถิ่น () แต่สำหรับชั้นเรียน
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 ]
ไม่ได้? เกิดขึ้นได้อย่างไรที่คุณไม่ทราบตัวแปรอินสแตนซ์ของคลาส