标签:
下面用一个例子来说明:
#coding:utf-8
"""
foo = decorator(foo) 语句的作用是将foo函数作为参数传递到decorator
返回值赋值给foo ,那么foo不再是原来的函数了
为了简化代码,python添加了装饰器这个语法糖
使得
@decorator
def foo():
print("欢迎参加黄哥python培训")
print("咨询qq:1465376564")
print("https://github.com/pythonpeixun/article/blob/master/index.md")
的效果和 foo = decorator(foo) 一样
"""
def decorator(func):
def inner():
print("begin func call")
func()
print("after func call")
return inner
def foo():
print("欢迎参加黄哥python培训")
print("咨询qq:1465376564")
print("https://github.com/pythonpeixun/article/blob/master/index.md")
@decorator
def foo2():
print("欢迎参加黄哥python培训")
print("咨询qq:1465376564")
print("https://github.com/pythonpeixun/article/blob/master/index.md")
foo = decorator(foo)
foo()
print("***************************")
foo2()
黄哥python远程视频培训班
https://github.com/pythonpeixun/article/blob/master/index.md
黄哥python培训试看视频播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
黄哥python培训_python初学者的第一步
http://www.tudou.com/programs/view/pZvrOt9RlmE/
如何捅破python编程的那层纸视频
http://www.tudou.com/programs/view/ppJv6Kf08Ac/
标签:
原文地址:http://my.oschina.net/pythonpeixun/blog/529801