查看此代码:def my_decorator(func):

return lambda: print("goodbye")

def greet():

print('hello')

result = my_decorator(greet)

result()

output:

goodbye

以下是完成相同任务的捷径:

^{pr2}$

@my_decorator语法接受其下面的函数greet,并进行以下调用:greet = my_decorator(greet)

必须定义my_decorator()函数,以便:它以函数作为参数。

返回函数。A Python decorator is a specific change to the Python syntax that

allows us to more conveniently alter functions and methods (and

possibly classes in a future version).

好的,假设您希望将添加到greet()函数所做的任何操作中:def my_decorator(func): # func = greet

def add_to_greet():

func() #

print('world') #

return add_to_greet

@my_decorator

def greet():

print('hello')

greet()

output:

hello

worldWhat does this line do @bottle.route('/static/')

好了,准备好了吗?如果@some_name语法指定了一个参数,例如:@wrapper('world')

def do_stuff():

首先python将执行以下调用:@wrapper('world')

def do_stuff():

...

#****HERE:

decorator = wrapper('world') #decorator is a newly created variable

必须将wrapper()函数定义为:以任何一个古老的论点为例,例如“世界”

返回一个函数:

将函数作为参数。在

返回函数。在

其次,python将执行调用:@wrapper('world')

def do_stuff():

...

decorator = wrapper('world')

#*****HERE:

do_stuff = decorator(do_stuff)

嘘!下面是一个例子:def wrapper(extra_greeting):

def my_decorator(func):

def add_to_greet():

func()

print(extra_greeting)

return add_to_greet

return my_decorator

@wrapper('world')

def greet():

print('hello')

greet()

output:

hello

world

现在,我们来分析一下这个装饰器:@bottle.route('/static/')

def server_static(filepath):bottle a module

route a function(or other callable) defined in the bottle module

'/static/' a route

瓶子模块可能是这样的:#bottle.py

def route(your_route): #your_route <= '/static/'

def my_decorator(func): #The decorator syntax will cause python to call this function with server_static as the argument

def do_stuff(filepath):

func(filepath) #Call the server_static() function with the part of the url that matched filepath

return do_stuff #This function will be called when your code calls server_static(...)

return my_decoratorIf its a fancy function call then why do it this way rather than just

calling the function?

先进的东西。在

注释:也许您忘了解释路由装饰器具体做了什么?@route('/hello')

def hello():

return "Hello World!"

The route() decorator binds a piece of code to an URL path. In this

case, we link the /hello path to the hello() function. This is called

a route (hence the decorator name) and is the most important concept

of this framework. You can define as many routes as you want. Whenever

a browser requests a URL, the associated function is called and the

return value is sent back to the browser. It’s as simple as that.

路径可以包含通配符:The simplest form of a wildcard consists of a name enclosed in angle

brackets (e.g. )....Each wildcard matches one or more

characters, but stops at the first slash (/).

The rule // matches as follows:Path Result

/save/123 {'action': 'save', 'item': '123'}

/save/123/ No Match

/save/ No Match

//123 No MatchFilters are used to define more specific wildcards, and/or transform

the matched part of the URL before it is passed to the callback. A

filtered wildcard is declared as The following standard filters are implemented:

:path matches all characters including the slash character in a non-greedy way and may be used to match more than one path

segment.

如何使用python装饰器_如何使用装饰器(瓶子.py)相关推荐

  1. python机器识别追踪_多目标追踪器:用OpenCV实现多目标追踪(C++/Python)

    原标题:多目标追踪器:用OpenCV实现多目标追踪(C++/Python) MultiTracker : Multiple Object Tracking using OpenCV (C++/Pyth ...

  2. 多项式朴素贝叶斯分类器_多项式朴素贝叶斯分类器的主题预测

    多项式朴素贝叶斯分类器 In Analytics Vidhya, Hackathon, there was a problem statement for text prediction of top ...

  3. spring 两次进入拦截器_过滤器和拦截器的 6 个区别,别再傻傻分不清了

    点击上方 肉眼品世界,选择 设为星标 深度价值体系传递 作者 :程序员内点事 来源 :toutiao.com/i6834310440495874563 毕竟这两种工具开发中用到的频率都相当高,应用起来 ...

  4. 如何听节拍器_如何用节拍器卡节拍?节拍器的使用方法!

    大家好,我是雅风. 这篇文章讲节拍器如何去使用,如何用节拍器卡节拍? 节拍器一般分为两种,一种机械运转上发条,一种电子. 首先我来讲一下节拍器使用方法. 一.机械节拍器. 机械节拍器 这种节拍器不需要 ...

  5. java 解析器_高性能Java解析器实现过程详解

    如果你没有指定数据或语言标准的或开源的Java解析器, 可能经常要用Java实现你自己的数据或语言解析器.或者,可能有很多解析器可选,但是要么太慢,要么太耗内存,或者没有你需要的特定功能.或者开源解析 ...

  6. 如何听节拍器_怎么听节拍器视频

    今天的吉他培训班,来给大家说说.时间就是一切吉他手们最喜欢那些新推出的小玩意--像变调夹.连线.踏板.电子调音器和效果器等等,但是节拍器总是受到忽视.然而,正确地(甚至是创造性地)使用节拍器的技巧,比 ...

  7. 几何着色器着色器_使用金属着色器制作第一个圆圈

    几何着色器着色器 Metal Shaders? Render Pipeline? Vertex Shaders? Fragment Shaders? If you were anything like ...

  8. 杰奇python采集器_极速杰奇采集器

    一,功能特色 本程序是杰奇小说系统的辅助采集器,使用的是杰奇后台采集规则,不像网上其他一些采集器,还需要重新编写采集规则,杰奇后台的采集规在网上随处都可以下到,并且本程序在所有windows系统上都可 ...

  9. nod32用户名获取器_内置调试器的nod​​ejs

    nod32用户名获取器 Let's imagine that I have a simple JavaScript file and I want to calculate the sum of in ...

最新文章

  1. linux 类型排序,Linux命令之排序命令sort使用实例
  2. iOS开发笔记(二):block循环引用
  3. ansible-playbook jdk安装
  4. 窗外传来嬉闹声,我默默关上窗
  5. ubuntu下面的背光键盘的使用
  6. [蓝桥杯][2018年第九届真题]倍数问题(贪心+思维)
  7. Redis 与 string 相关的常用命令
  8. sublime 关闭自动更新
  9. java 图片服务器 上传_Java实现把图片上传到图片服务器(nginx+vsftp)
  10. 邮件标题乱码问题解决一例
  11. css中表居中,CSS DIV中表格居中显示
  12. 优化理论15----进退法、python实现
  13. NLP实验一:形式语言和自动机
  14. Mobilenet——深度可分离卷积
  15. np.take()函数用法 python numpy
  16. 【为什么我在namesilo买的域名说我没有备案 】求大佬解答
  17. linux部署springBoot项目不间断运行
  18. 小熊派STM32-OTA+IOT教程
  19. 嵌入式学习笔记——ADC模数转换器
  20. 搭建服务器之PPTP

热门文章

  1. ubuntu新建python代码文件_ubuntu 下python的配置及更新方法和具体使用
  2. 测试公众号开发php,快速接入公众号开发
  3. 现在银行基金还有希望吗?
  4. 林业病虫害数据集介绍
  5. 交易系统单服务器tps,性能优化那些事之一,TPS和延时
  6. 【40-系统性能压力测试基本概念-相关性能指标HPSTPSQPSRT-安装Jmeter教程-JMeter测试流程-线程组-取样器-监视器-测试商城首页-JMeter Address 占用的问题】
  7. 【读书笔记】科学:无尽的前沿——科学是一种提出问题的方式
  8. github优秀项目源码汇总---Android
  9. Uip WebServer 实现
  10. 基于词典的细粒度情感分析