# coding=utf-8
"""装饰器:
"""
def deco(func):
"""无参数装饰器,接受一个函数,返回一个新的函数。
"""
def f(*args, **kwargs):
print "----", func.__name__
result = func(*args, **kwargs)
print "++++", func.__name__
f.__name__ = func.__name__
return result
return f
def deco_params(msg):
"""有参数装饰器,接受一个函数,返回一个无参数装饰器。
"""
def f(func):
def f2(*args, **kwargs):
print ">>>>", msg, func.__name__
result = func(*args, **kwargs)
print "<<<<", msg, func.__name__
return result
f2.__name__ = func.__name__
return f2
return f
@deco
@deco_params("ORANGE")
@deco_params("APPLE")
def f1_test(n):
print "orange", n
if __name__ == "__main__":
f1_test(30)
执行结果如下:
D:\ATMP\python>python test02.py
---- f1_test
>>>> ORANGE f1_test
>>>> APPLE f1_test
orange 30
<<<< APPLE f1_test
<<<< ORANGE f1_test
++++ f1_test