2010-01-29 2 views
16

파이썬 django에서 어떻게 객체의 inrospection을 출력합니까? 해당 객체의 모든 public 메소드 목록 (변수 및/또는 함수)?파이썬 django에서는 어떻게 객체의 인트로 스펙 션을 출력합니까? 해당 객체의 모든 public 메소드 목록 (변수 및/또는 함수)?

예컨대 :

 
def Factotum(models.Model): 
    id_ref = models.IntegerField() 

    def calculateSeniorityFactor(): 
    return (1000 - id_ref) * 1000 

나는 장고 모델의 공개 방법을 모두 말해 장고 쉘에서 명령 줄을 실행할 수 있어야합니다. 위의 실행 결과는 다음과 같습니다.

 
>> introspect Factotoum 
--> Variable: id_ref 
--> Methods: calculateSeniorityFactor 

답변

37

글쎄, 당신이 내성을 가질 수있는 것은 단지 하나가 아니라 많은 것입니다. 시작하는

좋은 일들은 다음과 같습니다

>>> help(object) 
>>> dir(object) 
>>> object.__dict__ 

또한 표준 라이브러리에있는 검사 모듈을 살펴.

모든 기지의 99 %가 당신에게 속해야합니다.

+0

파이썬에는 "introspect"모듈이 없습니다. –

+0

맞습니다. 결정된. –

6

사용 inspect : 당신이 정상적인 파이썬 쉘에서 괄호없이이를 호출 할 수있는 방법은 없습니다

import inspect 
def introspect(something): 
    methods = inspect.getmembers(something, inspect.ismethod) 
    others = inspect.getmembers(something, lambda x: not inspect.ismethod(x)) 
    print 'Variable:', # ?! what a WEIRD heading you want -- ah well, w/ever 
    for name, _ in others: print name, 
    print 
    print 'Methods:', 
    for name, _ in methods: print name, 
    print 

, 당신은 introspect(Factotum)을 사용해야합니다 ((클래스 Factotum 속성은 물론 현재 네임 스페이스에 수입 포함))이고 이 아님introspect Factotum 공백이 있습니다. 이것이 당신을 몹시 괴롭히는 경우 IPython을보고 싶을 수 있습니다.

관련 문제