[英]why does python decorator, is throwing recursion error?
def greet_decorator(print_name_function): # Decorating a function
def wrapper():
#function as an object
hello_name()
print('\tMr. Sunshine')
return wrapper
#my_obj = greet_decorator(hello_name)
#my_obj()
當我使用裝飾器遞歸錯誤時,任何人都可以向我詳細解釋這個概念
@greet_decorator
def hello_name():
print('Hello!')
hello_name()
您使用的greet_decorator
裝飾器替換hello_name
與功能的wrapper
。 wrapper
嘗試調用hello_name
,它本身就是。 這就是無限遞歸。
也許您打算調用原始(未修飾)函數,該函數作為參數print_name_function
。
def greet_decorator(print_name_function):
def wrapper():
print_name_function()
print('\tMr. Sunshine')
return wrapper
@greet_decorator
def hello_name():
print('Hello!')
hello_name()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.