2017-11-30 2 views
0

나는 아래와 같이 간단한 데코레이터를 사용합니다. 그러나 파이썬 파일을 가져 오면 즉시 실행되고 함수를 다시 호출 할 수 없습니다. 데코레이터는 어떻게 사용 되나요? 이 사용다른 파일에서 꾸미기 가져 오기 및 사용하기

def plain_decorator(func): 
    def decorated_func(): 
     print "Decorating" 
     func() 
     print "Decorated" 
    return decorated_func() 

@plain_decorator 
def hw(): 
    print "Hello Decorators!" 

>>> import decorator_ex2 as d 
Decorating 
Hello Decorators! 
Decorated 
>>> d.hw() 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
TypeError: 'NoneType' object is not callable 
>>> 

답변

2

시도는 당신이 외부 (plain_decorator)에서 반환 할 때이 기능을 당신의 내면 (decorated_func) 함수를 호출하기 때문에

def plain_decorator(func): 
    def decorated_func(): 
     print "Decorating" 
     func() 
     print "Decorated" 
    return decorated_func 

@plain_decorator 
def hw(): 
    print "Hello Decorators!" 
관련 문제