#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/6/3 13:36
# @Author : Ropon
# @File : 12_02.py
# 装饰器
# def warpper(fn):
# def inner():
# print("装饰函数执行前")
# fn() #利用闭包让函数常驻内存
# print("装饰函数执行后")
# return inner
#
# def test_func():
# print("这是一个测试函数")
#
# test_func = warpper(test_func)
# test_func()
# 需要传入参数装饰器
# def warpper(fn):
# def inner(*args, **kwargs):
# print("装饰函数执行前")
# fn(*args, **kwargs) #利用闭包让函数常驻内存
# print("装饰函数执行后")
# return inner
#
# def test_func(name, age):
# print(f"这是一个测试函数, 姓名是:{name}, 年龄是:{age}")
#
# test_func = warpper(test_func)
# test_func("ropon", 18)
# 带返回值得装饰器
# def warpper(fn):
# def inner(*args, **kwargs):
# print("装饰函数执行前")
# ret = fn(*args, **kwargs) #利用闭包让函数常驻内存
# print("装饰函数执行后")
# return ret
# return inner
#
# def test_func(name, age):
# print(f"这是一个测试函数, 姓名是:{name}, 年龄是:{age}")
# return {"name": name, "age": age}
#
# test_func = warpper(test_func)
# res = test_func("ropon", 18)
# print(res)
# 带参数的装饰器
# def warpper_out(fn, flag):
# def warpper(*args, **kwargs):
# if flag:
# print("装饰函数执行前")
# ret = fn(*args, **kwargs) # 利用闭包让函数常驻内存
# print("装饰函数执行后")
# else:
# ret = fn(*args, **kwargs) # 利用闭包让函数常驻内存
# return ret
# return warpper
#
# def test_func(name, age):
# print(f"这是一个测试函数, 姓名是:{name}, 年龄是:{age}")
# return {"name": name, "age": age}
#
# test_func = warpper_out(test_func, True)
# res = test_func("ropon", 18)
# print(res)
# 语法糖 @
# def warpper_out(flag):
# def warpper(fn):
# def inner(*args, **kwargs):
# if flag:
# print("装饰函数执行前")
# ret = fn(*args, **kwargs) #利用闭包让函数常驻内存
# print("装饰函数执行后")
# else:
# ret = fn(*args, **kwargs) # 利用闭包让函数常驻内存
# return ret
# return inner
# return warpper
#
# @warpper_out(False)
# def test_func(name, age):
# print(f"这是一个测试函数, 姓名是:{name}, 年龄是:{age}")
# return {"name": name, "age": age}
#
# res = test_func("ropon", 18)
# print(res)
# 多个装饰器
# def warpper_out1():
# pass
#
# def warpper_out2():
# pass
#
# def warpper_out3():
# pass
#
#
# @warpper_out1
# @warpper_out2
# @warpper_out3
# def func():
# pass
# 执行流程
# warpper_out1
# warpper_out2
# warpper_out3
# func
# warpper_out3
# warpper_out2
# warpper_out1