问题:route中的装饰器为什么感觉和平时使用的不太一样,装饰器带参数和不太参数有什么区别?被修饰的函数带参数和不带参数有什么区别?

测试1:装饰器不带参数,被修饰的函数也不带参数。

def log(func):
print"execute log"
print func
def use_log():
print "execute use log"
def wrapper():
print "start"
func()
print "end"
return
return wrapper
return 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
+
end

测试2:如果装饰器带参数,被修饰的函数不带参数

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"
return
return wrapper
return use_log @log('log')
def cal():
print "1+2" #这个时候数输出结果为:
execute log
log
execute use log
<function cal at 0x7f0c666b46e0>

 这个时候调用cal()那么将会执行wrapper()的函数体+cal()的函数体。

测试3:如果装饰器不带参数,被修饰的函数带参数

def log(func): #func 为cal()函数的地址
print"execute log"
def use_log(param): #param为cal的参数param
print "execute use log"
print param
def wrapper():
print "start"
func(param) #func 为cal()函数的地址,param为cal的参数param
print "end"
return
return wrapper
return 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:如果装饰器带参数,被修饰的函数也带参数。最复杂的情况。

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"
return
return wrapper
return 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 Flask

app = Flask(__name__)

@app.route('/')
def hello():
print 'execute hello function'
return 'Hello, World!'

@app.route('/')的代码如下:

    def route(self, rule, **options):
"""A decorator that is used to register a view function for a
given 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. Flask
itself assumes the name of the view function as
endpoint
:param options: the options to be forwarded to the underlying
:class:`~werkzeug.routing.Rule` object. A change
to Werkzeug is handling of method options. methods
is a list of methods this rule should be limited
to (``GET``, ``POST`` etc.). By default a rule
just listens for ``GET`` (and implicitly ``HEAD``).
Starting with Flask 0.6, ``OPTIONS`` is implicitly
added 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中装饰器的使用的更多相关文章

  1. python中装饰器的执行细节

    本文代码借用 廖雪峰的python教程(官网:http://www.liaoxuefeng.com/) 不了解装饰器的可以先看教程 直接上带参数装饰器的代码 def log(text): def de ...

  2. 8.Python中装饰器是什么?

    Python中装饰器是什么? A Python decorator is a specific change that we make in Python syntax to alter functi ...

  3. 第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法

    第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法 上节介绍了Python中类的静态方法,本节将结合案例详细说明相关内容. 一.    案例说明 本节定义了类Sta ...

  4. Python 入门之 Python三大器 之 装饰器

    Python 入门之 Python三大器 之 装饰器 1.开放封闭原则: (1)代码扩展进行开放 ​ 任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代 ...

  5. python函数与方法装饰器

    之前用python简单写了一下斐波那契数列的递归实现(如下),发现运行速度很慢. def fib_direct(n): assert n > 0, 'invalid n' if n < 3 ...

  6. guxh的python笔记三:装饰器

    1,函数作用域 这种情况可以顺利执行: total = 0 def run(): print(total) 这种情况会报错: total = 0 def run(): print(total) tot ...

  7. python设计模式之内置装饰器使用(四)

    前言 python内部有许多内建装饰器,它们都有特别的功能,下面对其归纳一下. 系列文章 python设计模式之单例模式(一) python设计模式之常用创建模式总结(二) python设计模式之装饰 ...

  8. python 3.x 的装饰器笔记

    今天学到了python的装饰器,感觉这个东西还是稍微有些复杂,所以记录下来,方便以后的查找.虽然标题是python 3.x的装饰器,但是我也没有怎么用过python 2.x,感觉上应该是和python ...

  9. Python函数编程——闭包和装饰器

    Python函数编程--闭包和装饰器 一.闭包 关于闭包,即函数定义和函数表达式位于另一个函数的函数体内(嵌套函数).而且,这些内部函数可以访问它们所在的外部函数中声明的所有局部变量.参数.当其中一个 ...

随机推荐

  1. Vue小项目二手书商城:(五)参考资料

    本项目基于vue2.5.2,如有错误,望指正. 完整程序:https://github.com/M-M-Monica/bukesi Vue.js官方文档:https://cn.vuejs.org/v2 ...

  2. react简书笔记一 环境, git 和 项目 关联

    1.. 建立git项目  ( 码云, github 都可以 ), 具体步骤: https://www.cnblogs.com/andy-lehhaxm/p/10720717.html 1.1  git ...

  3. element-UI表单验证

    转载自: 一.简单逻辑验证(直接使用rules) 实现思路 •html中给el-form增加 :rules="rules"•html中在el-form-item 中增加属性 pro ...

  4. 《R语言入门与实践》第一章:R基础

    前言 本章介绍了 R 语言的基础知识 界面: 使用命令 “ R “进行命令行的实时编译 对象 定义: 用于储存数据的,设定一个名称 格式: a <- 1:6 命名规则: 规则1:不能以数字开头规 ...

  5. Oracle解决ora-01653 无法通过1024扩展

    综合上述检查结果,可断定遇到的问题是因为可能性1—表空间不足导致.解决办法也就是扩大表空间 扩大表空间的四种方法: 1.增加数据文件 ALTER TABLESPACE ***_TRD ADD DATA ...

  6. 浅谈UML中常用的几种图——类图

    在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization),关联(Association),聚合(Aggregation),组合(Composit ...

  7. OO面向对象第一单元总结

    OO面向对象第一单元总结(表达式求导) 写在前面: 魔鬼课程oo第一单元终于结束,当终究要落笔总结,竟不知从何写起…… 回首再去看第一次的作业,你会满足于那时的幸福,或许,这就是成长吧! 千言万语,一 ...

  8. istio路由配置

    istio路由配置   istio的代理配置参考文档: 中文文档: https://istio.io/zh/docs/reference/config/istio.networking.v1alpha ...

  9. 使用Visual Studio Installer 2015打包WPF程序

    前言 做过WPF项目,就少不了要将程序打包部署到客户现场,因为一般长时间不会更新打包程序,每次变动较大需要重新配置打包程序时,就会有些生疏,不那么得心应手.为了方便记忆,记录到博客中. 准备 因为做过 ...

  10. redis最全配置讲解

    #redis.conf# Redis configuration file example.# ./redis-server /path/to/redis.conf ################# ...