Python3_装饰器
#! /usr/bin/env python3
def login(func):
def inner(arg):
print("I am in inner")
func(arg)
return inner
@login #一个语法糖,tv变量名替换func,name替换arg
def tv(name):
print("{}".format(name))
def login2(func_Name):#login2(*argc)表示任意个数的参数,**dic表示为字典类型
def inner2(*argc,**dic):
print("I am in Inner2!!")
func_Name(*argc,**dic)
return inner2
#tv = login(tv)
tv("Elix")
#多参数装饰器
def fun1(arg1,arg2):#定义函数1
print("Hi,I am fun1{}{}".format(arg1,arg2))
def fun2(arg1,arg2):#定义函数2
print("I am fun2{}{}".format(arg1,arg2))
def fun3(arg1,arg2):
print("I am fun3{}{}".format(arg1,arg2))
def login3(fun1,fun2,fun3):#装饰器将fun1,fan2装入,可装入多个函数
def inner1(main_fun):#封装的主函数
def inner2(arg1,arg2):#封装的参数
fun1(arg1,arg2)
st = main_fun(arg1,arg2)
print(st)
fun2(arg1,arg2)
fun3(arg1,arg2)
return inner2
return inner1
@login3(fun1,fun2,fun3)
def main_f(name1,name2):#主函数
print("{}{},I am in main_fun".format(name1,name2))
return "Main_FUN is ME"
main_f("JT","ELA")
装饰器:
def login(func):
def inner(arg):
print("I am in inner")
func(arg)
return inner
@login #一个语法糖,tv变量名替换func,name替换arg
def tv(name):
print("{}".format(name))
执行步骤:当解释器执行到@login ——>def login(func):——> def inner(arg):——> return inner——>tv = inner(重定向)——>tv(name):——>inner(name)——>print("I am in inner")——>tv(name)——>END