티스토리 뷰

공부

[Python] loop of class member variables

승가비 2021. 5. 31. 17:29
728x90
class Example(object):
    bool143 = True
    bool2 = True
    blah = False
    foo = True
    foobar2000 = False

example = Example()
members = [attr for attr in dir(example) if not callable(getattr(example, attr)) and not attr.startswith("__")]
print members   

https://stackoverflow.com/questions/1398022/looping-over-all-member-variables-of-a-class-in-python

 

looping over all member variables of a class in python

How do you get a list of all variables in a class thats iteratable? Kind of like locals(), but for a class class Example(object): bool143 = True bool2 = True blah = False foo = Tru...

stackoverflow.com

 

728x90
댓글