python flask route中装饰器的使用
問題:route中的裝飾器為什么感覺和平時(shí)使用的不太一樣,裝飾器帶參數(shù)和不太參數(shù)有什么區(qū)別?被修飾的函數(shù)帶參數(shù)和不帶參數(shù)有什么區(qū)別?
測(cè)試1:裝飾器不帶參數(shù),被修飾的函數(shù)也不帶參數(shù)。
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"此時(shí)輸出為:
execute log <function cal at 0x7fa64535f668> #這里的function為cal的函數(shù)地址如果執(zhí)行cal()那么將會(huì)使用use_log函數(shù),返回的是wrapper()
execute log <function cal at 0x7f42ee7a4668> execute use log如果執(zhí)行cal()的返回值,那么將執(zhí)行cal()函數(shù)體的內(nèi)容
result = cal() result()結(jié)果為:
execute log <function cal at 0x7f38dc4d1668> execute use log start 1+2 end測(cè)試2:如果裝飾器帶參數(shù),被修飾的函數(shù)不帶參數(shù)
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' def log(func): #這里的func為裝飾器函數(shù)參數(shù)print"execute log"print func #這里的func為裝飾器函數(shù)參數(shù)def use_log(func): #這里的func為函數(shù)cal()的地址print "execute use log"print func #這里的func為函數(shù)cal()的地址def wrapper():print "start"func()print "end"returnreturn wrapperreturn use_log@log('log') def cal():print "1+2"#這個(gè)時(shí)候數(shù)輸出結(jié)果為: execute log log execute use log <function cal at 0x7f0c666b46e0>這個(gè)時(shí)候調(diào)用cal()那么將會(huì)執(zhí)行wrapper()的函數(shù)體+cal()的函數(shù)體。
測(cè)試3:如果裝飾器不帶參數(shù),被修飾的函數(shù)帶參數(shù)
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' def log(func): #func 為cal()函數(shù)的地址print"execute log"def use_log(param): #param為cal的參數(shù)paramprint "execute use log"print paramdef wrapper():print "start"func(param) #func 為cal()函數(shù)的地址,param為cal的參數(shù)paramprint "end"returnreturn wrapperreturn use_log@log def cal(param):print "1+2"result = cal('cal') result()#執(zhí)行的結(jié)果為: execute log execute use log cal start 1+2 end #如果注掉最后兩行代碼,那么只有輸出 execute log測(cè)試4:如果裝飾器帶參數(shù),被修飾的函數(shù)也帶參數(shù)。最復(fù)雜的情況。
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' def log(func): #func為裝飾器的參數(shù)print"execute log"def use_log(func): #func為cal的函數(shù)地址print "execute use log"print func #func為cal的函數(shù)地址def wrapper(param): #param為cal的參數(shù)print "start"func(param)print "end"returnreturn wrapperreturn use_log@log('test') def cal(param):print "1+2"result = cal('cal')#執(zhí)行的結(jié)果為: execute log execute use log <function cal at 0x7f23bbc6d6e0> start 1+2 end經(jīng)過上面的分析之后,再看flask中使用的是哪種情況:
樣例代碼:
from flask import Flaskapp = Flask(__name__)@app.route('/') def hello():print 'execute hello function'return 'Hello, World!'@app.route(’/’)的代碼如下:
''' 遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和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可以看到裝飾器的參數(shù)為‘/’,被修飾的函數(shù)為:hello(),所以這里屬于第二種情況,即使不調(diào)用hello()函數(shù),decorator的函數(shù)體也是被執(zhí)行的,也就是說,只要使用裝飾器添加了路由規(guī)則,那么就會(huì)被加入到map中形成映射關(guān)系。
總結(jié)
以上是生活随笔為你收集整理的python flask route中装饰器的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python :给类或者类的对象添加打印
- 下一篇: python flask 如何修改默认端