python flask route中装饰器的使用
生活随笔
收集整理的這篇文章主要介紹了
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 :给类或者类的对象添加打印
- 下一篇: python flask 如何修改默认端