python flask route中装饰器的使用
问题:route中的装饰器为什么感觉和平时使用的不太一样,装饰器带参数和不太参数有什么区别?被修饰的函数带参数和不带参数有什么区别?
测试1:装饰器不带参数,被修饰的函数也不带参数。
def log(func):print"execute log"print funcdef use_log():print "execute use log"def wrapper():print "start"func()print "end"returnreturn wrapperreturn use_log@log
def cal():print "1+2"
此时输出为:
execute log
<function cal at 0x7fa64535f668> #这里的function为cal的函数地址
如果执行cal()那么将会使用use_log函数,返回的是wrapper()
execute log
<function cal at 0x7f42ee7a4668>
execute use log
如果执行cal()的返回值,那么将执行cal()函数体的内容
result = cal()
result()
结果为:
execute log
<function cal at 0x7f38dc4d1668>
execute use log
start
1+2
end
测试2:如果装饰器带参数,被修饰的函数不带参数
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def log(func): #这里的func为装饰器函数参数print"execute log"print func #这里的func为装饰器函数参数def use_log(func): #这里的func为函数cal()的地址print "execute use log"print func #这里的func为函数cal()的地址def wrapper():print "start"func()print "end"returnreturn wrapperreturn use_log@log('log')
def cal():print "1+2"#这个时候数输出结果为:
execute log
log
execute use log
<function cal at 0x7f0c666b46e0>
这个时候调用cal()那么将会执行wrapper()的函数体+cal()的函数体。
测试3:如果装饰器不带参数,被修饰的函数带参数
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def log(func): #func 为cal()函数的地址print"execute log"def use_log(param): #param为cal的参数paramprint "execute use log"print paramdef wrapper():print "start"func(param) #func 为cal()函数的地址,param为cal的参数paramprint "end"returnreturn wrapperreturn use_log@log
def cal(param):print "1+2"result = cal('cal')
result()#执行的结果为:
execute log
execute use log
cal
start
1+2
end
#如果注掉最后两行代码,那么只有输出
execute log
测试4:如果装饰器带参数,被修饰的函数也带参数。最复杂的情况。
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def log(func): #func为装饰器的参数print"execute log"def use_log(func): #func为cal的函数地址print "execute use log"print func #func为cal的函数地址def wrapper(param): #param为cal的参数print "start"func(param)print "end"returnreturn wrapperreturn use_log@log('test')
def cal(param):print "1+2"result = cal('cal')#执行的结果为:
execute log
execute use log
<function cal at 0x7f23bbc6d6e0>
start
1+2
end
经过上面的分析之后,再看flask中使用的是哪种情况:
样例代码:
from flask import Flaskapp = Flask(__name__)@app.route('/')
def hello():print 'execute hello function'return 'Hello, World!'
@app.route(’/’)的代码如下:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def route(self, rule, **options):"""A decorator that is used to register a view function for agiven URL rule. This does the same thing as :meth:`add_url_rule`but is intended for decorator usage::@app.route('/')def index():return 'Hello World'For more information refer to :ref:`url-route-registrations`.:param rule: the URL rule as string:param endpoint: the endpoint for the registered URL rule. Flaskitself assumes the name of the view function asendpoint:param options: the options to be forwarded to the underlying:class:`~werkzeug.routing.Rule` object. A changeto Werkzeug is handling of method options. methodsis a list of methods this rule should be limitedto (``GET``, ``POST`` etc.). By default a rulejust listens for ``GET`` (and implicitly ``HEAD``).Starting with Flask 0.6, ``OPTIONS`` is implicitlyadded and handled by the standard request handling."""def decorator(f):endpoint = options.pop('endpoint', None)self.add_url_rule(rule, endpoint, f, **options)print "this param has been accessed"return f
return decorator
可以看到装饰器的参数为‘/’,被修饰的函数为:hello(),所以这里属于第二种情况,即使不调用hello()函数,decorator的函数体也是被执行的,也就是说,只要使用装饰器添加了路由规则,那么就会被加入到map中形成映射关系。
python flask route中装饰器的使用相关推荐
- python中装饰器修复技术_python3之装饰器修复技术@wraps
普通函数 def f(): """ 这是一个用来测试装饰器修复技术的函数 """ print("哈哈哈") if __n ...
- python中的json函数_python中装饰器、内置函数、json的详解
装饰器 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 先看简单例子: def run(): time.sleep(1 ...
- 8.Python中装饰器是什么?
Python中装饰器是什么? A Python decorator is a specific change that we make in Python syntax to alter functi ...
- python装饰器使用教学,Python教程|简单上手Python中装饰器的使用
如何打造极简主义风格网站教程 .极简主义与其他设计风格<极简主义设计之美> 这篇文章的一部分是讲极简主义是如何与其他设计风格很好配合的.可以把极简主义当作一种属性,而它可以添加或结合其他. ...
- python return用法_初学Python要了解什么 装饰器知识汇总有哪些
初学Python要了解什么?装饰器知识汇总有哪些?在Python学习过程中,有多种方法对函数和类进行加工,相对于其它方式,装饰器语法简单,代码可读性高.因此,装饰器在Python项目中有广泛的应用,比 ...
- [转载] Python学习笔记——用装饰器decorator和Memoization记忆化提高效率,原理讲清楚了
参考链接: 在Python中使用装饰器进行记忆 Python学习笔记--用装饰器decorator和Memoization记忆化提高效率 装饰器Memoization记忆化运用`functools`中 ...
- itext生成pdf paragraph 自动换行_reportlab高级制作多格式PDF和python的class和装饰器复习
1 说明 1.1 reportlab高级制作多格式PDF:即包含文字.表格.柱状图. 1.2 复习python的class和装饰器的高级基础知识. 1.3 列表转换字符串.读取txt文件等python ...
- Python之闭包与装饰器
闭包 由于闭包这个概念比较难以理解,尤其是初学者来说,相对难以掌握,所以我们通过示例去理解学习闭包. 给大家提个需求,然后用函数去实现:完成一个计算不断增加的系列值的平均值的需求. 例如:整个历史中的 ...
- python实现计时器(装饰器)
1, Python时间装饰器_BurningSilence的博客-CSDN博客_python 时间装饰器 from functools import wraps import time# time装饰 ...
最新文章
- Java面向对象:覆写与重载
- 英特尔溢价4倍收购还遭拒!RISC-V芯片公司SiFive说想独立上市
- IDT 信息设计工具使用
- MySQL带IN关键字的子查询
- how is SAP UI5 applyBackendSearchPattern being called
- ubantu java编辑器_Linux Ubuntu中最好的代码编辑器 程序员都这么看吗?
- 字符串匹配算法(BF RK)
- 使用Eclipse创建maven项目
- Linux下安装MyEclipse和Tomcat服务器详解,以及我安装过程中所出现的问题以及解决办法,并实现一个web小程序
- 数据库笔记07:实施数据完整性
- 6月6号=》80页-100页
- iOS bugly集成报错
- Win10查看笔记本电池损耗和健康度
- FlashFXP使用阿里云ECS私钥登录
- AspNetPager常用属性及用法 / URLRewrite伪静态与AspNetPager分页控件的结合
- 关于target is null for setProperty的问题总结
- 澳洲2022人口普查结果出炉--华人占比开始下降
- Android系统充电指示灯无响应
- 微软的winget包管理器
- appollo-二次规划ST速度优化