python的语法糖@和闭包的简单理解
@:@func1修饰的函数func3
会作为func1
的参数
闭包:三个条件:
- 有内嵌函数
- 内嵌函数调用了外部函数的变量
- 外部函数返回内部函数地址(函数名不加括号)
外部函数称为闭包函数。
在调用闭包函数时func3=func1
,这样func3
就获得了func2
的地址,但是由于不在同一个命名空间,func3
修改不了func1
中的变量所以有个保护作用。
def func1(func3):#这个叫decorator
def func2():#里面这个函数其实叫wrapper
print('before the func,execute sth...')
#这里就写在执行func3前执行的功能,其实就实现func3功能的扩展
func3
print('after the func,execute sth...')
return func2#返回wrapper的地址
#调用:
@func1
def func3():
pass
func3()
#用@func1之后就使在调用func3时,
#看wrapper中,func3()执行前后都会再执行一些东西,就实现了对func3的扩展
#另一种情况:
def dec():
def wrapper(g):
print('before')
g()
print('after')
return wrapper
@dec()
def test():
print('Hello!')
#这里的test不能被调用,因为@dec()相当于是#@wrapper,直接把test()给了wrapper。
注意:
@func1
def func3():
pass
func3
相当于
def func3():
pass
func3=func1(func3)
func3()
叠层:
@decorator1
@decorator2
def func4():
pass
func4()
这里先decorator2,再decorator1
注意如果没有写func4()
,即没有调用func4()
,则不会执行wrapper()
易错
带参数的情况:
def decorator_maker(a,b):
print('I am the decorator maker!')
def decorator(func):
print('I am the decorator!')
def wrapper():
print('I am the wrapper !')
func()
print('wrapper calling is over!')
print('decorator calling is over!')
return wrapper
print('decrator_maker calling is over !')
return decorator
@decorator_maker('A',"B")
def func5():
print('I am the func1')
func5()
函数名和该函数的意义相同。
为了给decorator
传递参数,可以向decorator_maker
传递,由于命名空间:三个函数都可以使用该参数,此外wrapper
还可以使用被修饰的函数func5
- 注意
@decorator_maker()
这里是调用函数 - 可以在
decorator_maker()
中多定义几个装饰器 - 注意
@
在该模块被导入时就被装饰了,和import
一样。