装饰器-wrapper
我跟别人说我精通python,别人问我wrapper是啥,我说不知道,尼玛,原来wrapper就是装饰器,熟的不得了啊,英语真是我的克星啊。
闭包 closure
在认识装饰器之前先认识下闭包
闭包,顾名思义就是把什么东西封闭在保内,什么东西呢?变量和函数。
在一个函数里装了另一个函数,里面那个函数称为内部函数,外面那个函数称为外部函数,
在内部函数里,对在外部作用域(非全局作用域)里的变量进行引用,这个内部函数就称为闭包
定义在外部函数内但被内部函数引用或调用的变量称为自由变量,所以闭包又被称为引用了自由变量的函数。
内部函数和自由变量同时存在构建一个闭包实体,闭包的作用就是使闭包实体可以脱离创建它的环境运行,就是变量和函数脱离了创建它的环境依然存在,而且可执行,这点跟面向对象有些类似。
闭包的语法是一个函数A内创建了一个函数B,并且函数A返回了函数B,函数B就是闭包,函数A内的变量叫自由变量,包括A的参数
示例代码
def counter(start_at=):
print('act')
count=[start_at] # 自由变量
def incr(): # 内部函数
count[]+=
return count[]
return incr # 返回一个函数对象 count=counter() # act
print #
print count() # # count 和 incr 脱离了创建它的环境,依然可以运行
print count() # count2=counter() # act
print #
print count2() #
print count() #
闭包的作用
1. 闭包实际上是对数据或者数据操作的封装
2. 闭包可以实现一些通用的功能,它是装饰器的基础。
装饰器
装饰器本质上是个函数,一个用来包装函数的函数,返回被包装的函数对象。
被包装的函数需要作为装饰器函数的参数。
装饰器以语法糖@开头,形式如下
@decorator(dec_opt_args)
def func2Bdecorated(func_opt_args):
并且可以有多个装饰器
@deco2
@deco1
def func():
pass 等价于 func=deco2(deco1(func))
普通方式实现类似装饰器的功能,以帮助理解
def deco(func):
print("before myfunc() called.")
func()
print(" after myfunc() called.")
return func def myfunc():
print(" myfunc() called.") myfunc = deco(myfunc)
# before myfunc() called.
# myfunc() called.
# after myfunc() called. myfunc() # myfunc() called.
myfunc() # myfunc() called.
使用语法糖@装饰函数,注意这并不是装饰器
def deco(func):
print("before myfunc() called.")
func()
print("after myfunc() called.")
return func @deco
def myfunc():
print("myfunc() called.") myfunc()
# before myfunc() called.
# myfunc() called.
# after myfunc() called.
# myfunc() called.
print("*"*5)
myfunc() # myfunc() called.
可以看到第一次执行时@起了作用,第二次执行时@没起作用。为什么呢,往下看
真正的装饰器来了
无参装饰器
def deco(func):
print(33)
def _deco():
print("before myfunc() called.")
func()
print(" after myfunc() called.")
return _deco @deco
def myfunc():
print(" myfunc() called.")
return 'ok' myfunc()
#
# before myfunc() called.
# myfunc() called.
# after myfunc() called.
myfunc()
# before myfunc() called.
# myfunc() called.
# after myfunc() called.
可以看到每次执行@都起了作用,但是33只被打印了一次,又是为什么呢?
明明是执行了两次一模一样的操作,结果却不同,看来有必要深入理解一下装饰器了。
深入理解装饰器
之前讲到闭包类似于面向对象,而装饰器基于闭包,也应该类似于面向对象咯,或许吧,类似嘛,我又没说是,所以应该没错,
为什么扯这么多,因为我要用class来解释上面的问题。
对上面的无参装饰器分析一
## 上述过程类似这样
def myfunc():
print(" myfunc() called.")
return 'ok' bb = deco(myfunc) #
bb()
# before myfunc() called.
# myfunc() called.
# after myfunc() called.
bb()
# before myfunc() called.
# myfunc() called.
# after myfunc() called.
把装饰器转成普通函数,就明了了:
装饰器内的操作在创建装饰器实例时已经运行,这可以理解为class的实例化,如果在实例化时有print操作,在实例调用时不会再有
对上面的无参装饰器分析二
def deco(func):
a = 3
def _deco():
print("before myfunc() called.")
func()
print(a)
print(" after myfunc() called.")
return _deco @deco
def myfunc():
print(" myfunc() called.")
return 'ok' myfunc()
# before myfunc() called.
# myfunc() called.
#
# after myfunc() called.
myfunc()
# before myfunc() called.
# myfunc() called.
#
# after myfunc() called.
装饰器传递的是自由变量和闭包,可以理解为class的实例属性和方法,在实例调用时,属性一直存在。
myfunc()第一次运行时相当于初始化了装饰器,后面只是调用实例,虽然它没有生成实例对象,在这点上不同于class。
总结
装饰器函数真的类似于面向对象
装饰器在第一次运行时相当于实例化class,实例化时可以有操作和属性,操作不被传递,属性被传递
装饰器不需要创建实例对象,运行即相当于实例化class
装饰器传递的是自由变量和属性,装饰器函数内的操作不被传递
装饰器的各种语法
有参装饰器
def deco(func):
def _deco(a, b):
print("before myfunc() called.")
ret = func(a, b)
print(" after myfunc() called. result: %s" % ret)
return ret
return _deco @deco
def myfunc(a, b):
print(" myfunc(%s,%s) called." % (a, b))
return a + b myfunc(1, 2)
# before myfunc() called.
# myfunc(1,2) called.
# after myfunc() called. result: 3
myfunc(3, 4)
# before myfunc() called.
# myfunc(3,4) called.
# after myfunc() called. result: 7
装饰器带参数
外面加一层
def deco(arg):
def _deco(func):
def __deco():
print("before %s called [%s]." % (func.__name__, arg))
func()
print(" after %s called [%s]." % (func.__name__, arg))
return __deco
return _deco @deco("mymodule")
def myfunc():
print(" myfunc() called.") @deco("module2")
def myfunc2():
print(" myfunc2() called.") myfunc()
myfunc2()
装饰器带类参数
装饰器的参数是个类,也可以是实例,或者其他
class locker:
def __init__(self):
print("locker.__init__() should be not called.") @staticmethod
def acquire():
print("locker.acquire() called.(这是静态方法)") @staticmethod
def release():
print(" locker.release() called.(不需要对象实例)") def deco(cls):
'''cls 必须实现acquire和release静态方法'''
def _deco(func):
def __deco():
print("before %s called [%s]." % (func.__name__, cls))
cls.acquire()
try:
return func()
finally:
cls.release()
return __deco
return _deco @deco(locker)
def myfunc():
print(" myfunc() called.") myfunc()
myfunc()
被装饰的函数属性发生了变化
def deco(func):
def myfunc(x):
return func(x)
return myfunc @deco
def test1(x):
return x+1 print(test1(4)) #
print(test1.__name__) # myfunc 名字并非真实名字
名字并非真正函数的名字,而是装饰器函数里被装饰的函数的名字
保留被装饰的函数的属性
import time
import functools def timeit(func):
@functools.wraps(func) # 此句就是用来保留被装饰的函数的属性的 ,其余跟普通装饰器一样
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
return wrapper @timeit
def foo():
print 'in foo()' foo() # used: 5.13182184074e-06
print foo.__name__ # foo
name属性被保留
参考资料:
http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html
装饰器-wrapper的更多相关文章
- python14 1.带参装饰器 | wrapper 了了解 # 2.迭代器 ***** # 可迭代对象 # 迭代器对象 # for迭代器 # 枚举对象
## 复习 '''函数的嵌套定义:在函数内部定义另一个函数 闭包:被嵌套的函数 -- 1.外层通过形参给内层函数传参 -- 2.验证执行 开放封闭原则: 功能可以拓展,但源代码与调用方式都不可以改变 ...
- day14带参装饰器,迭代器,可迭代对象 , 迭代器对象 ,for迭代器 , 枚举对象
复习 ''' 函数的嵌套定义:在函数内部定义另一个函数 闭包:被嵌套的函数 -- 1.外层通过形参给内层函数传参 -- 2.验证执行 开放封闭原则: 功能可以拓展,但源代码与调用方式都不可以改变 装饰 ...
- python学习日记(函数--装饰器)
楔子 前提,我有一段代码(一个函数). import time def run_time(): time.sleep(0.1) print('我曾踏足山巅') 需求1:现在,我想计算这段代码的运行时间 ...
- python当中的装饰器
1.装饰器 首先我们来说一下一个软件的设计原则:开闭原则,又被称为开发封闭原则,你的代码对功能的扩展是开放的,你的程序对修改源代码是封闭的.这样的软件设计思路可以更好的维护和开发. 开放:对功能扩展开 ...
- python基础(8)--迭代器、生成器、装饰器
1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优 ...
- Python入门-装饰器初始
今天我们就围绕一个来展开,那就是:装饰器 一.装饰器 在说装饰器之前,我们先说一个软件设计的原则:开闭原则,又被称为开放封闭原则,你的代码对功能的扩展是开放的,你的程序对修改源代码是封闭的,这样的软件 ...
- 巨蟒python全栈开发-第15天 装饰器
一.今日内容总览 关于函数的装饰器1.装饰器(重点,难点)(要求:反复写,代码不多但是很绕) 开闭原则:(比如,菜单是拆散的,一点点搞的,用友拆散自己的功能,以后就不用开发了) (1)对功能的扩展开放 ...
- python 复习函数 装饰器
# 函数 —— 2天 # 函数的定义和调用 # def 函数名(形参): #函数体 #return 返回值 #调用 函数名(实参) # 站在形参的角度上 : 位置参数,*args,默认参数(陷阱),* ...
- 13.Python略有小成(装饰器,递归函数)
Python(装饰器,递归函数) 一.开放封闭原则 软件面世时,不可能把所有的功能都设计好,再未来的一两年功能会陆续上线,定期更新迭代,软件之前所用的源代码,函数里面的代码以及函数的调用方式一般不 ...
随机推荐
- CentOS中与网络相关的常用
CentOS中与网络相关的常用配置文件 1. 常见的网络配置文件 /etc/hosts 本地域名解析表,用于解析主机名.对应于win系统中的C:\Windows\System32\ ...
- Java编码 蛇形矩阵的构建与遍历输出
一.蛇形矩阵的构建,并按行输出 例: 输入:n, 生成n*n的蛇形矩阵 1 2 3 8 9 4 7 6 5 输出:1 2 3 8 9 4 7 6 5 java编码 public static void ...
- python-flask-SQLAlchemy-Utils组件
SQLAlchemy-Utils,提供choice功能 定义: # pip3 install sqlalchemy-utils from sqlalchemy_utils import ChoiceT ...
- CF-503div2-A/B/C
A. New Building for SIS time limit per test 1 second memory limit per test 256 megabytes input stand ...
- hdu-6341-搜索
Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- 11. Container With Most Water C++
知识点:双指针遍历大大减少不必要的比较和计算 方法1:Brute Force(执行时间惨不忍睹,共进行n(n-1)/2次比较) class Solution { public: int maxArea ...
- git commit -am "remark" 提交
一.前言 假如你昨晚把本地文件a.html提交到远程库,今早发现还有补充的内容,于是添加了新的内容到a.html,并且还在本地还多添加了“几个文件”,那么怎么使用git来把这些文件一并提交到远程库呢? ...
- dubbo初认知(dubbo和springCloud关系,在微服务架构中的作用等)(持续更新中)
一:dubbo是什么? dobbuo是阿里开源的一个高性能优秀的服务框架, 可通过高性能的 RPC 实现服务的输出和输入功能,使得应用可以和 高性能的rpc实现输入和输出的功能,可以了 Spring ...
- 【转】js生成接口请求参数签名加密
js生成接口请求参数签名加密 签名算法规则: 第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式(即key1=v ...
- JTA 使用 MySQL 分布式事务
假定在MySQL实例1上有表 create table person( id int, name ) ) MySQL实例2上也有一张同样的表,现在从实例1中的 person 表中删除一条数据,并把这条 ...