1.例子1
打印结果:
33
在例子1中,f1() 与f2() 都没有被调用,只执行了print(33)
f1与f2,是没有被调用的,但是如果f1 和 f2 上面有注解,就会被调用执行。
2.python 利用装饰器实现类似于flask路由
注释类 Grass
运行入口
运行app.run()
然后输入 :
/home
/index
/mine
分析实现逻辑:
当运行app.run() 时,代码运行逻辑是
1、先执行1 实例化Grass对象
2、装饰器@app.router("/home") 运行
3、装饰器@app.router("/index") 运行
4、最后才是app.run() 运行
装饰器@app.router("/home") 运行逻辑
装饰器@app.router("/home"),运行
@app.router("/home") 对应 def router(self,url):
1、“/home” 传给 def router(self,url),url =“/home”
2、@app.router("/home"),运行得到 decorator函数
3、然后将home函数作为参数,传递给decorator函数
4、self.add_url_to_map(url,f)
将 url(“/home”) 与 home 函数组成 字典。
在字典中,字符串 /home 对应home 函数