Почему функция Internal_text_function не определена при использовании декораторов?Python

Программы на Python
Anonymous
Почему функция Internal_text_function не определена при использовании декораторов?

Сообщение Anonymous »

Я не понимаю, почему я не могу получить доступ к имени внутренней функции, украшенной внешней функцией?
это вызывает эту ошибку:NameError: имя «inner_text_function» не определено
def outer_wrapper_function(func) -> str:
print(f"This is the outer function: named -|{outer_wrapper_function.__name__}|- and located at -|{outer_wrapper_function}")
print(f" |-->This is the inner function: named -|{func.__name__}|- and located at -|{func}")
textwraped: str = func().upper()
print(f"Text wrapped: {textwraped}")
return textwraped

#Passing a function as argument var_1:

def inner_text_function() -> str:
print(f" |-->This is the inner function: named -|{inner_text_function.__name__}|- and located at -|{inner_text_function}")
textwrap = "this is some texts to be wrapped ****"
print(f"Text un-wrapped: {textwrap}")
return textwrap
text_function = outer_wrapper_function(func = inner_text_function)
print(text_function)

#Now printing the inner_text_function not decorated with @ suntax:
text_function = inner_text_function
print(text_function)

#Passing a function as argument var_2:

@outer_wrapper_function
def inner_text_function(text = "NOW") -> str:
print(f" |-->This is the inner function: named -|{inner_text_function.__name__}|- and located at -|{inner_text_function}")
textwrap = "this is some texts to be wrapped ****".replace('****', text)
print(f"Text un-wrapped: {textwrap}")
return textwrap

#Now printing the inner_text_function alredy decorated with @ suntax:
text_function = inner_text_function
print(text_function, type(inner_text_function))

#it work if i pass the inner function as the var_1 but it raise that error with te var_2. why?


Подробнее здесь: https://stackoverflow.com/questions/788 ... decorators

Вернуться в «Python»