2014-12-26 3 views
1

bar 외부의 도우미 함수를 이동하거나 FUNC_TO_CALL 문자열을 전달한 다음 문자열을 기반으로 함수를 선택하는 것 외에 다음 작업을 수행 할 수있는 방법이 있습니까?내부 도우미 함수의 Python3 전달 매개 변수

#foo.py 
def bar(FUNC_TO_CALL) 
    def helper_function_1(): 
     ... 
    def helper_function_2(): 
     ... 

    FUNC_TO_CALL() 

#main.py 
foo.bar(bar.helper_function_1) #<- HOW DO I PASS IN THIS HELPER INTERNAL TO BAR AS ARGUMENT? 

은 내가 bar에 전달되는 매개 변수를 사용하여 전화를 걸 헬퍼의 많은 기능 bar 있습니다. 대안은 모든 도우미를 모듈 수준으로 이동하는 것이지만 bar 외부에서는 쓸모 없기 때문에 지저분합니다. 도우미 기능 bar의 조금 일부인 경우

preprocessing... 
helper 1 
preprocessing... 
helper 2 

비록 :

+0

'bar'함수가 원하는 함수를 호출하는 것 외에 다른 작업을 수행합니까? – Dettorer

+0

네, 다른 일을합니다. – Tommy

답변

1

당신은 bar 밖으로 장식 만들기의 가능성을 연구 할 수 있습니다이 :

def bar(helper): 
    def process(): 
     print('preprocessing...') 
     # Anything you need to do prior to calling the helper function 
     helper() 

    return process 

@bar 
def helper_function_1(): 
    print('helper 1') 

@bar 
def helper_function_2(): 
    print('helper 2') 

if __name__ == '__main__': 
    helper_function_1() 
    helper_function_2() 

이 출력을 제공합니다 그 일은별로 의미가 없습니다.