1.首先普及语法@:
python 2.4以后,增加了@符号修饰函数对函数进行修饰,python3.0/2.6又增加了对类的修饰。
@符号的意义:
1.1函数修饰:
@dec1
@dec2
def test(arg):
pass
等同于
def test(arg):
pass
dec1(dec2(test(arg)))
1.2 函数修饰(带参):
@dec1(arg1,arg2)
def test(arg):pass
等同于
def test(arg):pass
dec1(arg1,arg2)(test(arg))
1.3 注意点:
由于@的粗暴性,如果在装饰器中返回需要装饰的对象的话,定义目标函数时装饰器就会被调用一次(执行这个定义事),且之后永远不被调用。
例子:
def func_deco1(func):
print("before %s" % func.__name__)
func(object)
print("after %s" % func.__name__)
return func
class A(object):
def f1(self):
print("A: f1")
def f2(self):
print("A: f2")
pass
class B(object):
@func_deco1
def f1(self):
print("B: f1")
@func_deco1
def f2(sllf):
print("B: f2")
pass
print("------------------------You are a pig-------------------------------")
o1=A()
o1.f1()
o1.f2()
print("------------------------You are a pig-------------------------------")
o2=B()
o2.f1()
o2.f2()
注意,定义类B的时候,装饰器函数执行,以后不执行
before f1
B: f1
after f1
before f2
B: f2
after f2
------------------------You are a pig-------------------------------
A: f1
A: f2
------------------------You are a pig-------------------------------
B: f1
B: f2
2.将@用于装饰器:
2.1 直接用于函数
def deco(func):
def _deco():
print("before %s" % func.__name__)
func()
print("after %s" % func.__name__)
pass
return _deco
#注意这里返回值是内部函数,包装的时候返回值是一个带有装饰器的函数,而不是函数本身,且不调用任何函数
@deco
def f1():
print("f1")
print("------------------------You are a pig-------------------------------")
f1()
f1()
print("------------------------You are a pig-------------------------------")
print("------------------------You are a pig-------------------------------")
def f2():
print("f2")
print("------------------------You are a pig-------------------------------")
f=deco(f2)
print("------------------------You are a pig-------------------------------")
f()
print("------------------------You are a pig-------------------------------")
f()
------------------------You are a pig-------------------------------
before f1
f1
after f1
before f1
f1
after f1
------------------------You are a pig-------------------------------
------------------------You are a pig-------------------------------
------------------------You are a pig-------------------------------
------------------------You are a pig-------------------------------
before f2
f2
after f2
------------------------You are a pig-------------------------------
before f2
f2
after f2
2.2 装饰器用于类函数(与1.3区分 注意)
def func_deco(func):
def _func_deco(object):
print("before %s" % func.__name__)
func(object)
print("after %s" % func.__name__)
return _func_deco
class A(object):
def f1(self):
print("A: f1")
def f2(self):
print("A: f2")
pass
class B(object):
@func_deco
def f1(self):
print("B: f1")
@func_deco
def f2(sllf):
print("B: f2")
pass
print("------------------------You are a pig-------------------------------")
o1=A()
o1.f1()
o1.f2()
print("------------------------You are a pig-------------------------------")
o2=B()
o2.f1()
o2.f2()
------------------------You are a pig-------------------------------
before f1
B: f1
after f1
before f2
B: f2
after f2
2.3 装饰器用于类