美文网首页
Python入门与进阶(12-10)

Python入门与进阶(12-10)

作者: buaishengqi | 来源:发表于2018-05-16 21:20 被阅读3次
# 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