객체의 변수&속성을 출력하는 방법
조회수 9501회
1 답변
-
- 인자가 주어지지 않으면 현재 local scope에 있는
name
을 저장한list
를return
해줍니다. - 인자가 주어진 경우엔 그
object
에 있는attribute
를string
형태로 저장한list
를return
해줍니다. object
가module object
인 경우, 리스트는 모듈의 어트리뷰트 이름을 저장하고 있습니다.object
가type
이나class object
인 경우, 리스트는 해당 클래스와base
클래스들의 어트리뷰트 이름을 저장하고 있습니다.object
가__dir__()
를 지원하지 않으면, 대신__dict__
어트리뷰트 등에서 정보를 모아옵니다.object안에서
__getattr__()
을 따로 정의한 경우에는dir()
이return
한list
는 완전하지 않을 수도 있습니다list
의 원소는 알파벳 순서로 정렬되어있습니다
vars()는
module
,class
,instance
, 그 외__dict__
어트리뷰트가 있는object
의__dict__
어트리뷰트를return
해줍니다.class myClass(object): def __init__(self): pass def myFunc1(self): pass from pprint import pprint l = dir(myClass) print "---print l---" print l print "\n\n---pprint(l)---" pprint(l) print "\n\n---pprint(l, indent=2)---" pprint(l, indent=2)
결과)
---print l--- ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'myFunc1'] ---pprint(l)--- ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'myFunc1'] ---pprint(l, indent=2)--- [ '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'myFunc1']
- 인자가 주어지지 않으면 현재 local scope에 있는
댓글 입력