Python入门与进阶(12-10)
# 12-10 装饰器 二
import time
def decorator(func):#定义装饰器的外部函数
def wrapper():#装饰器内部定义一个函数(被封装的)
print(time.time())
func()
return wrapper
def f1():
print('this is a function')
f = decorator(f1)
f()
# 打印结果如图1
# 注意与上一节对比,发现没有什么大的不同
import time
def decorator(func):#定义装饰器的外部函数
def wrapper():#装饰器内部定义一个函数(被封装的)
print(time.time())
func()
return wrapper
@decorator
def f1():
print('this is a function')
# f = decorator(f1)加上@语法糖以后,这部分可以省略了
f1()#直接调用f1 ,这个就是装饰器的优势!!
# 打印结果如图1
本文标题:Python入门与进阶(12-10)
本文链接:https://www.haomeiwen.com/subject/fwvmdftx.html
网友评论