# Functions within Functions
def hello(name = 'Jose'):
def greet():
return '\t This indise the greet() function'
def welcome():
return '\t This indise the welcome() function'
if name == 'Jose':
return greet
else:
return welcome
x = hello()
print(x())
# 결과
This inside the greet() function
# Functions as Arguments
def hello():
return "Hi! Jose!"
def other(func):
print("Other code goes here!")
print(func())
other(hello)
# 결과
Other code goes here!
Hi! Jose!
# Wrap Function
def new_decorator(func):
def wrap_func():
print("Code here, before executing the func")
func()
print("Code here will execute after the func()")
return wrap_func
def func_needs_decorator():
print("This function needs a decorator!")
func_needs_decorator = new_decorator(func_needs_decorator)
func_needs_decorator()
# 결과
Code here, before executing the func
This function needs a decorator!
Code here will execute after the func()
# 장식자 표기법을 사용해서 수동으로 데코레이터 할당을 대신함
def new_decorator(func):
def wrap_func():
print("Code here, before executing the func")
func()
print("Code here will execute after the func()")
return wrap_func
@new_decorator
def func_needs_decorator():
print("This function needs a decorator!")
func_needs_decorator()
# 결과
Code here, before executing the func
This function needs a decorator!
Code here will execute after the func()
댓글 영역