반응형
반복 가능한 클래스의 모든 변수 목록을 어떻게 얻습니까? 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
해결 방법
dir(obj)
객체의 모든 속성을 제공합니다. 메서드 등에서 멤버를 직접 필터링해야합니다.
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
당신에게 줄 것입니다 :
['blah', 'bool143', 'bool2', 'foo', 'foobar2000']
참조 페이지 https://stackoverflow.com/questions/1398022
반응형
'파이썬' 카테고리의 다른 글
파이썬 ImportError : 6이라는 모듈이 없습니다. (0) | 2021.01.31 |
---|---|
파이썬 Python-sock.recv를 문자열로 변환 (0) | 2021.01.31 |
파이썬 중단하고 기능 계속 (0) | 2021.01.31 |
파이썬 3D Numpy 배열을 2D로 (0) | 2021.01.31 |
파이썬 전역으로 설치된 패키지를 virtualenv 폴더로 가져 오는 방법 (0) | 2021.01.31 |
댓글