装饰器 decorator
def document_it(func):
def new_function(*args, **kwargs):
print('Running function:', func.__name__)
print('Positional arguments:', args)
print('Keyword arguments:', kwargs)
result = func(*args, **kwargs)
print('Result:', result)
return result
return new_function #def add_ints(a, b):
# return a + b
#cooler_add_ints = document_it(add_ints)
#cooler_add_ints(3, 5)
#('Running function:', 'add_ints')
#('Positional arguments:', (3, 5))
#('Keyword arguments:', {})
#('Result:', 8) #@document_it
#def add_ints(a, b):
# return a + b #add_ints(3, 5) def square_it(func):
def new_function(*args, **kwargs):
result = func(*args, **kwargs)
return result * result
return new_function #@square_it
#@document_it
#def add_ints(a, b):
# return a + b #add_ints(3, 5) #('Running function:', 'add_ints')
#('Positional arguments:', (3, 5))
#('Keyword arguments:', {})
#('Result:', 8) @document_it
@square_it
def add_ints(a, b):
return a + b add_ints(3, 5) #('Running function:', 'new_function')
#('Positional arguments:', (3, 5))
#('Keyword arguments:', {})
#('Result:', 64) #靠近函数定义的装饰器最先执行
def log(func):
def wrapper(*args, **kw):
print('call %s():' % func.__name__)
return func(*args, **kw)
return wrapper @log
def now():
print('2015-3-25') now() #call now():
#2015-3-25
如果decorator本身需要传入参数
def log(text):
def decorator(func):
def wrapper(*args, **kw):
print('%s %s():' % (text, func.__name__))
return func(*args, **kw)
return wrapper
return decorator @log('execute')
def now():
print('2015-3-25') now() print(now.__name__) #execute now():
#2015-3-25
#wrapper
不需要编写wrapper.__name__ = func.__name__这样的代码,Python内置的functools.wraps就是干这个事的

相当于:
now = log(now)
now = log('execute')(now)

装饰器 decorator的更多相关文章
- python 装饰器(decorator)
装饰器(decorator) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 装饰器(decorator)是一种高级Python语 ...
- python语法32[装饰器decorator](转)
一 装饰器decorator decorator设计模式允许动态地对现有的对象或函数包装以至于修改现有的职责和行为,简单地讲用来动态地扩展现有的功能.其实也就是其他语言中的AOP的概念,将对象或函数的 ...
- Python的程序结构[8] -> 装饰器/Decorator -> 装饰器浅析
装饰器 / Decorator 目录 关于闭包 装饰器的本质 语法糖 装饰器传入参数 1 关于闭包 / About Closure 装饰器其本质是一个闭包函数,为此首先理解闭包的含义. 闭包(Clos ...
- Python_高阶函数、装饰器(decorator)
一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...
- python 语法之 装饰器decorator
装饰器 decorator 或者称为包装器,是对函数的一种包装. 它能使函数的功能得到扩充,而同时不用修改函数本身的代码. 它能够增加函数执行前.执行后的行为,而不需对调用函数的代码做任何改变. 下面 ...
- python函数编程-装饰器decorator
函数是个对象,并且可以赋值给一个变量,通过变量也能调用该函数: >>> def now(): ... print('2017-12-28') ... >>> l = ...
- 就谈个py 的装饰器 decorator
很早很早就知道有这么个 装饰器的东西,叫的非常神秘. 包括c# 和 java 中都有这个东西, c#中叫做attribut 特性,java中叫做Annotation 注解,在偷偷学习c#教程的时候, ...
- Day4 闭包、装饰器decorator、迭代器与生成器、面向过程编程、三元表达式、列表解析与生成器表达式、序列化与反序列化
一.装饰器 一.装饰器的知识储备 1.可变长参数 :*args和**kwargs def index(name,age): print(name,age) def wrapper(*args,**k ...
- 【Angular专题】 (3)装饰器decorator,一块语法糖
目录 一. Decorator装饰器 二. Typescript中的装饰器 2.1 类装饰器 2.2 方法装饰器 2.3 访问器装饰器 2.4 属性装饰器 2.5 参数装饰器 三. 用ES5代码模拟装 ...
- 浅析python中的装饰器decorator
最近学习python,其中decorator比较难理解,遂写一篇来总结供后续查阅. 定义一个函数,想在运行时动态的改变函数的功能,又不想改变函数本身的代码,可以使用高阶函数(可以使用函数作为参数) 装 ...
随机推荐
- Android View 布局流程(Layout)完全解析
前言 上一篇文章,笔者详细讲述了View三大工作流程的第一个,Measure流程,如果对测量流程还不熟悉的读者可以参考一下上一篇文章.测量流程主要是对View树进行测量,获取每一个View的测量宽高, ...
- linux的history命令设置
history的历史记录,同一个用户的各个会话,读取到的内容也是不一样的,原因是它读取的是shell会话缓存里的内容.只有当用户退出当前会话的时候,会话里的缓存内容才会写入~/.bash_histor ...
- 转:在CentOS下编译安装GCC
转:https://teddysun.com/432.html 在CentOS下编译安装GCC 技术 秋水逸冰 发布于: 2015-09-02 更新于: 2015-09-02 6519 次围观 ...
- react 监听 移动端 手机键盘 enter 事件
处理方法: (1)html书写 form标签中去掉action参数,定义onSubmit方法,如下所示: /** * 搜索框 组件 */ import React,{PureComponent} fr ...
- react-redux 和 redux-saga 小结
react-redux 将 store 绑定到 props 上,便于全局调用. redux-saga 是将 redux 的同步转换为异步. 注: dispatch 到 saga , saga 匹配行为 ...
- Irrlicht 3D Engine 笔记系列之 教程4 - Movement
作者: i_dovelemon 日期: 2014 / 12 / 16 来源: CSDN 主题: Event Receiver, Animator, Framerate independent move ...
- js 宽和高
网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offs ...
- 可空类型Nullable
Nullable类型: 值类型变量默认为0,不可空,为了使它可空,出现了Nullable类型,类型前面加? 变为引用类型 值类型是没有null值的,比如int,DateTime,它们都有默认值.举个 ...
- WPF02(concept)
(转自http://www.cnblogs.com/huangxincheng/archive/2012/06/17/2552322.html)这些天从项目上接触到了wpf,感觉有必要做一个笔记,首篇 ...
- Java 序列化Serializable具体解释(附具体样例)
Java 序列化Serializable具体解释(附具体样例) 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描写叙述的过程:反序列化deserializa ...