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

Python入门与进阶(12-8)

作者: buaishengqi | 来源:发表于2018-05-16 20:18 被阅读14次

12-8 装饰器 一

# 12-8 装饰器 一
import time 
def f1():
    print(time.time())
    print('this is a function')
f1()
# 打印结果如图1

# 如果有一百个函数需要打印出时间怎么办?
# 对修改是封闭的,对扩展是开放的。需求如果改变,尽量不要去修改函数,要去修改扩展,适应需求变更
# 看看下面的代码!

import time 
def f1():
    print('this is a function')

def f2():
    print('this is a function')

def print_current_time(func):
    print(time.time())
    func
print_current_time(f1)
print_current_time(f2)
# 打印结果如图2
# 不用装饰器还是有缺点的
1.jpg 2.jpg

相关文章

网友评论

      本文标题:Python入门与进阶(12-8)

      本文链接:https://www.haomeiwen.com/subject/rcymdftx.html