2014-09-23 2 views
-1

데코레이터에 값 목록을 전달하려고합니다. 데코레이터로 장식 된 모든 함수는 다른 값 목록을 전달합니다.데코레이터에 인수 전달

from decorator import decorator 
def dec(func, *args): 
    // Do something with the *args - I guess *args contains the arguments 
    return func() 

dec = decorator(dec) 

@dec(['first_name', 'last_name']) 
def my_function_1(): 
    // Do whatever needs to be done 

@dec(['email', 'zip']) 
def my_function_2(): 
    // Do whatever needs to be done 

그러나,이 작동하지 않습니다 - 나는 decorator 파이썬 라이브러리 여기

을 사용하고하는 것은 내가하려고했던 것입니다. 오류가 발생합니다 - AttributeError: 'list' object has no attribute 'func_globals'

어떻게하면됩니까?

+0

https://stackoverflow.com/questions/5929107/python-decorators-with-parameters –

답변

0

당신은 장식 라이브러리

def custom_decorator(*args, **kwargs): 
    # process decorator params 
    def wrapper(func): 
     def dec(*args, **kwargs): 
      return func(*args, **kwargs) 
     return dec 
    return wrapper 

@custom_decorator(['first_name', 'last_name']) 
def my_function_1(): 
    pass 
@custom_decorator(['email', 'zip']) 
def my_function_2(): 
    pass 
+0

그 멋진없이 구현할 수 있습니다. 어쨌든 이것은'decorator' 라이브러리로 구현 될 수 있습니까? – Siddharth