python学习之老男孩python全栈第九期_day012知识点总结

# def wrapper(f):
# def inner(*args,**kwargs):
# print('在被装饰的函数执行之前做的事')
# res = f(*args,**kwargs)
# print('在被装饰的函数执行之后做的事')
# return res
# return inner
# @wrapper # holiday = wrapper(holiday) 这时holiday == inner
# def holiday(day):
# print('全体放假%s天'%day)
# return '好开心'
# ret = holiday(3)
# print(ret)

# def outer(*args,**kwargs):
# print(args,kwargs)
# print(*args)
# def inner(*args):
# print('inner:', args)
# inner(*args)
# outer(1,2,3,4,a = 1,b = 2)

# def wahaha():
# '''
# 一个打印娃哈哈的函数
# :return:
# '''
# print('娃哈哈')
# print(wahaha.__name__) # 查看字符串格式的函数名
# print(wahaha.__doc__) # document (查看函数的注释)


# from functools import wraps
# def wrapper(f): # f = holiday
# @wraps(f) # 加上这句话之后, holiday 就是holiday 不是inner了
# def inner(*args,**kwargs):
# print('在被装饰的函数执行之前做的事')
# res = f(*args,**kwargs)
# print('在被装饰的函数执行之后做的事')
# return res
# return inner
# @wrapper # holiday = wrapper(holiday) 这时holiday == inner ,这时还没加上面那句话
# def holiday(day):
# print('全体放假%s天'%day)
# return '好开心'
# ret = holiday(3)
# print(holiday.__name__) # 输出inner,加了上面那句话之后输出 holiday

# ---------------------------------------------------------------------------------------------------- #

# 带参数的装饰器

# 500个函数
#
# import time
# FLAG = True
# def timmer_out(flag):
# '''
# 计算机函数执行时间
# :param flag:flag为真时,运行这个函数,为假时不运行
# :return:
# '''
# def timmer(func):
# def inner(*args,**kwargs):
# if FLAG:
# start_time = time.time()
# ret = func(*args,**kwargs)
# end_time = time.time()
# print(end_time - start_time)
# return ret
# else:
# ret = func(*args, **kwargs)
# return ret
# return inner
# return timmer
# @timmer_out(FLAG) # 先调用timmer_out(FLAG),返回的结果为timmer, 加了@ 之后就等于 @timmer,就等于wahaha = timmer(wahaha)
# # 上个式子等价于:
# # timmer = timmer_out(FLAG)
# #@timmer
#
# def wahaha():
# time.sleep(0.1)
# print('娃哈哈')
# @timmer_out(FLAG)
# def erguotou():
# time.sleep(0.1)
# print('二锅头')
# wahaha()
# erguotou()


# 多个装饰器装饰一个函数

#
# def wrapper1(func): # func --> f
# def inner1():
# print('wrapper1,before func')
# func() # f
# print('wrapper1,after func')
# return inner1
#
# def wrapper2(func): # func --> inner1
# def inner2():
# print('wrapper2, before func')
# func() # 其实是 inner1()
# print('wrapper2, after func')
# return inner2
#
# @wrapper2 # f = wrapper2(f) --> wrapper2(inner1), 返回的inner2,故 f == inner2
# @wrapper1 # f = wrapper1(f) 返回的inner1,故 f == inner1
#
# # 先找最近的函数,wrapper2没找到, wrapper1先找的f(),然后wrapper2又找的
# def f():
# print('in f')
#
# f() # --> inner2()

# ---------------------------------------------------------------------------------- #
# 三个

def wrapper1(func):
def inner1():
print('wrapper1,before func')
ret = func()
print('wrapper1,after func')
return ret
return inner1

def wrapper2(func):
def inner2():
print('wrapper2, before func')
ret = func()
print('wrapper2, after func')
return ret
return inner2

def wrapper3(func):
def inner3():
print('wrapper3, before func')
ret = func()
print('wrapper3, after func')
return ret
return inner3

@wrapper3
@wrapper2
@wrapper1
def f():
print('in f')
return '哈哈哈'
print(f())

# 记录用户的登录情况 and 计算这个函数的执行时间
posted @ 2018-07-10 23:11  李培冠  阅读(401)  评论(0编辑  收藏  举报