- 裝飾器, 不明思議, 作用是來裝飾一個東西的, 注意, 是裝飾, 不是修改. 個人感覺, 就好比化妝, 只是在人本來的面貌上做了一些修飾, 並沒有真正改變人的樣子.
import time
def student():
print('print student name')
def student():
print_time()
print('print student name')
def print_time():
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
def student():
print('print student name')
def print_time(func):
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
func()
print_time(student)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
student()
def decorator(func):
def wrapper():
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
func()
return wrapper
def student():
print('print student name')
f = decorator(student)
f()
def decorator(func):
def wrapper():
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
func()
return wrapper
@decorator
def student():
print('print student name')
student()
def decorator(func):
def wrapper(name):
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
func(name)
return wrapper
@decorator
def student(name):
print('print student ' + name)
student('xiaosheng')
def decorator(func):
def wrapper(*args):
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
func(*args)
return wrapper
@decorator
def student(name):
print('print student ' + name)
student('xiaosheng')
本作品採用《CC 協議》,轉載必須註明作者和本文連結