2019-04-01 15:35:58 1505浏览
今天扣丁学堂Python培训老师给大家分享推荐一篇关于python装饰器的详细介绍,文中通过示例代码介绍的非常详细,首先Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼。
def f1():
print('f1 called')
def f2():
print('f2 called')
def w1(func):
def inner():
print('...验证权限...')
func()
return inner
@w1
def f1():
print('f1 called')
@w1
def f2():
print('f2 called')
f1()
f2()
...验证权限... f1 called ...验证权限... f2 called
def w1(fun):
print('...装饰器开始装饰...')
def inner():
print('...验证权限...')
fun()
return inner
@w1
def test():
print('test')
test()
...装饰器开始装饰... ...验证权限... test
test = w1(test)
def makeBold(fun):
print('----a----')
def inner():
print('----1----')
return '<b>' + fun() + '</b>'
return inner
def makeItalic(fun):
print('----b----')
def inner():
print('----2----')
return '<i>' + fun() + '</i>'
return inner
@makeBold
@makeItalic
def test():
print('----c----')
print('----3----')
return 'hello python decorator'
ret = test()
print(ret)
----b---- ----a---- ----1---- ----2---- ----c---- ----3---- <b><i>hello python decorator</i></b>
def w_say(fun):
"""
如果原函数有参数,那闭包函数必须保持参数个数一致,并且将参数传递给原方法
"""
def inner(name):
"""
如果被装饰的函数有行参,那么闭包函数必须有参数
:param name:
:return:
"""
print('say inner called')
fun(name)
return inner
@w_say
def hello(name):
print('hello ' + name)
hello('wangcai')
say inner called hello wangcai
def w_add(func):
def inner(*args, **kwargs):
print('add inner called')
func(*args, **kwargs)
return inner
@w_add
def add(a, b):
print('%d + %d = %d' % (a, b, a + b))
@w_add
def add2(a, b, c):
print('%d + %d + %d = %d' % (a, b, c, a + b + c))
add(2, 4)
add2(2, 4, 6)
add inner called 2 + 4 = 6 add inner called 2 + 4 + 6 = 12
def w_test(func):
def inner():
print('w_test inner called start')
func()
print('w_test inner called end')
return inner
@w_test
def test():
print('this is test fun')
return 'hello'
ret = test()
print('ret value is %s' % ret)
w_test inner called start this is test fun w_test inner called end ret value is None
def w_test(func):
def inner():
print('w_test inner called start')
str = func()
print('w_test inner called end')
return str
return inner
@w_test
def test():
print('this is test fun')
return 'hello'
ret = test()
print('ret value is %s' % ret)
w_test inner called start this is test fun w_test inner called end ret value is hello
def func_args(pre='xiaoqiang'):
def w_test_log(func):
def inner():
print('...记录日志...visitor is %s' % pre)
func()
return inner
return w_test_log
# 带有参数的装饰器能够起到在运行时,有不同的功能
# 先执行func_args('wangcai'),返回w_test_log函数的引用
# @w_test_log
# 使用@w_test_log对test_log进行装饰
@func_args('wangcai')
def test_log():
print('this is test log')
test_log()
...记录日志...visitor is wangcai this is test log简单理解,带参数的装饰器就是在原闭包的基础上又加了一层闭包,通过外层函数func_args的返回值w_test_log就看出来了,具体执行流程在注释里已经说明了。
def w_test(func):
def inner(*args, **kwargs):
ret = func(*args, **kwargs)
return ret
return inner
@w_test
def test():
print('test called')
@w_test
def test1():
print('test1 called')
return 'python'
@w_test
def test2(a):
print('test2 called and value is %d ' % a)
test()
test1()
test2(9)
test called test1 called test2 called and value is 9
class Test(object):
def __call__(self, *args, **kwargs):
print('call called')
t = Test()
print(t())
call called
class Test(object):
def __init__(self, func):
print('test init')
print('func name is %s ' % func.__name__)
self.__func = func
def __call__(self, *args, **kwargs):
print('装饰器中的功能')
self.__func()
@Test
def test():
print('this is test func')
test()
test init func name is test 装饰器中的功能 this is test func
想要了解更多关于Python和人工智能方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的Python开发环境搭建视频,Python培训后的前景无限,行业薪资和未来的发展会越来越好的,扣丁学堂老师精心推出的Python视频教程定能让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python技术交流群:279521237。
【关注微信公众号获取更多学习资料】 【扫码进入Python全栈开发免费公开课】