decorator
# 無參僅需2層函式
def log_print(func):
def wrapper(*args, **kwargs):
print("before " + func.__name__)
result = func(*args, **kwargs)
print("after " + func.__name__)
return result
return wrapper
# 有參3層函式
def log_print2(params: bool = False):
def decorator(func):
def wrapper(*args, **kwargs):
print(f"bool:{params}")
print("before " + func.__name__)
result = func(*args, **kwargs)
print("after " + func.__name__)
return result
return wrapper
return decorator
def test(a, b):
print(f"入參項為:{a},{b}")
return a + b
@log_print2(params=True)
def test2(a, b):
print(f"入參項為:{a},{b}")
return a + b
test(1, 2)
test2(1, 2)`