谈谈python修饰器
前言
对python的修饰器的理解一直停留在"使用修饰器把函数注册为事件的处理程序"的层次,也是一知半解;这样拖着不是办法,索性今天好好整理一下关于python修饰器的概念及用法。
介绍
装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等。装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用。概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能。
功能
我们首先从一个简单的例子说起,这个例子是stackflow上的一个问题,如何通过使用如下的代码实现输出<b><i>Hello</i></b>
:
@makebold
@makeitalic
def say():
return "Hello"
先看一下答案:
def makebold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
@makebold
@makeitalic
def hello():
return "hello world"
print hello() ## 返回 <b><i>hello world</i></b>
这里的@makebold
和@makeitalic
似乎给Hello加上了一层包装(or修饰),这就是修饰器最明显的体现。
从需求谈起
初期,我写了一个函数
def foo():
print 'in foo()'
foo()
为了检查这个函数的复杂度(在网络编程中程序的延时还是很重要的),需要测算运算时间,增加了计算时间的功能有了下面的代码:
import time
def foo():
start = time.clock()
print 'in foo()'
end = time.clock()
print 'Time Elapsed:', end - start
foo()
这里只是写了一个函数,如果我想测量多个函数的延时,由于必须知道start与end,所以必须写在程序的开头与结尾,难道每一个程序都这样复制粘贴么?固然可行,但是,我们可以通过设计模式中将功能与数据部分分离一样,将这个测量时间的函数分离出去,就像C++中我们可以将这个测量时间的函数变为一个类,通过调用这个类,赋予不同的函数来测量不同的函数的运行时长。在python中,由于函数实际上就是对象,所以可以利用类似的方法实现:
import time
def foo():
print 'in foo()'
def timeit(func):
start = time.clock()
func()
end =time.clock()
print 'Time Elapsed:', end - start
timeit(foo)
这里func()就可以指定函数了,但是如果我不想填这个函数或者这个功能函数并不能修改成类似的形式怎么办?我们需要的是最大限度的少改动:
import time
def foo():
print 'in foo()'
# 定义一个计时器,传入一个,并返回另一个附加了计时功能的方法
def timeit(func):
# 定义一个内嵌的包装函数,给传入的函数加上计时功能的包装
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'Time Elapsed:', end - start
# 将包装后的函数返回
return wrapper
foo = timeit(foo) #可以直接写成@timeit + foo定义,python的"语法糖"
foo()
在这个代码中,timeit(foo)不是直接产生调用效果,而是返回一个与foo参数列表一致的函数,此时此foo非彼foo!因为此时的foo具有了timeit的功效,简单来说就是能够让你在装饰前后执行代码而无须改变函数本身内容,装饰器是一个函数,而其参数为另外一个函数。
一个有趣的"汉堡"让你了解顺序
顺序在修饰器还是非常重要的,利用一个代码展示一下:
def bread(func) :
def wrapper() :
print "</''' '''\>"
func()
print "<\______/>"
return wrapper
def ingredients(func) :
def wrapper() :
print "#tomatoes#"
func()
print "~salad~"
return wrapper
def sandwich(food="--ham--") :
print food
sandwich()
#输出 : --ham--
sandwich = bread(ingredients(sandwich))
sandwich()
#输出:
#</''' '''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>
加上语法糖,代码可以更简洁:
def bread(func) :
def wrapper() :
print "</''' '''\>"
func()
print "<\______/>"
return wrapper
def ingredients(func) :
def wrapper() :
print "#tomatoes#"
func()
print "~salad~"
return wrapper
@bread
@ingredients
def sandwich(food="--ham--") :
print food
sandwich()
拓展
内置修饰器
内置的装饰器有三个,分别是staticmethod、classmethod和property,作用分别是把类中定义的实例方法变成静态方法、类方法和类属性。
对有参函数进行修饰
一个参数
如果原函数有参数,那闭包函数必须保持参数个数一致,并且将参数传递给原方法
def w1(fun):
def wrapper(name):
print("this is the wrapper head")
fun(name)
print("this is the wrapper end")
return wrapper
@w1
def hello(name):
print("hello"+name)
hello("world")
# 输出:
# this is the wrapper head
# helloworld
# this is the wrapper end
多个参数测试:
def w2(fun):
def wrapper(*args,**kwargs):
print("this is the wrapper head")
fun(*args,**kwargs)
print("this is the wrapper end")
return wrapper
@w2
def hello(name,name2):
print("hello"+name+name2)
hello("world","!!!")
#输出:
# this is the wrapper head
# helloworld!!!
# this is the wrapper end
有返回值的函数
def w3(fun):
def wrapper():
print("this is the wrapper head")
temp=fun()
print("this is the wrapper end")
return temp #要把值传回去呀!!
return wrapper
@w3
def hello():
print("hello")
return "test"
result=hello()
print("After the wrapper,I accept %s" %result)
#输出:
#this is the wrapper head
#hello
#this is the wrapper end
#After the wrapper,I accept test
有参数的修饰器
直接上代码:
def func_args(pre='xiaoqiang'):
def w_test_log(func):
def inner():
print('...记录日志...visitor is %s' % pre)
func()
return inner
return w_test_log
# 带有参数的修饰器能够起到在运行时,有不同的功能
# 先执行func_args('wangcai'),返回w_test_log函数的引用
# @w_test_log
# 使用@w_test_log对test_log进行修饰
@func_args('wangcai')
def test_log():
print('this is test log')
test_log()
#输出:
#...记录日志...visitor is wangcai
# this is test log
通用修饰器
对每个类型都有一个修饰器形式,怎么记得下来?所以就有了这个"万能修饰器":
def w_test(func):
def inner(*args, **kwargs):
ret = func(*args, **kwargs)
return ret
return inner
@w_test
def test():
print('test called')
@w_test
def test1():
print('test1 called')
return 'python'
@w_test
def test2(a):
print('test2 called and value is %d ' % a)
test()
test1()
test2(9)
# 输出:
#test called
#test1 called
#test2 called and value is 9
类修饰器
当创建一个对象后,直接去执行这个对象,那么是会抛出异常的,因为他不是callable,无法直接执行,但进行修改后,就可以直接执行调用:
class Test(object):
def __call__(self, *args, **kwargs):
print('call called')
t = Test()
print(t())
# 就可以直接执行
直接对类进行修饰:
class Test(object):
def __init__(self, func):
print('test init')
print('func name is %s ' % func.__name__)
self.__func = func
def __call__(self, *args, **kwargs):
print('this is wrapper')
self.__func()
@Test
def test():
print('this is test func')
test()
#输出:
# test init
# func name is test
# this is wrapper
# this is test func
后记
先介绍到这里,大致也对修饰器有了一定的理解。后面自己会结合自己的项目继续深入学习。
谈谈python修饰器的更多相关文章
- Python修饰器的函数式编程
Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...
- Python修饰器
Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...
- Python修饰器的函数式编程(转)
From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...
- python 修饰器 最好的讲解
Python的修饰器的英文名叫Decorator,修饰器就是对一个已有的模块做一些“修饰工作”,比如在现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能 ...
- python修饰器(装饰器)以及wraps
Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼. 装饰器的引入 初期及问题的诞生 假如现在在一个公司,有A B C三个业务部门,还有S一 ...
- python 修饰器(decorator)
转载:Python之修饰器 - 知乎 (zhihu.com) 什么是修饰器,为什么叫修饰器 修饰器英文是Decorator, 我们假设这样一种场景:古老的代码中有几个很是复杂的函数F1.F2.F3.. ...
- Python 修饰器
描述:对于函数foo,使用修饰器修饰,在执行foo函数的同时统计执行时间.这样其他函数都可以使用此修饰器得到运行时间. (有返回值和没有返回值的函数要用不同的修饰器似乎) (对于有返回值的函数,不确定 ...
- Python修饰器讲解
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 文章先由stackoverflow上面的一个问题引起吧,如果使 ...
- python修饰器各种实用方法
This page is meant to be a central repository of decorator code pieces, whether useful or not <wi ...
随机推荐
- 线段树Final版本
结构体是个好东西... 看着逼格很高 #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- E比昨天更多的棒棒糖(Easy+Hrad)(华师网络赛)(DP||母函数||背包优化)
Time limit per test: 2.0 seconds Memory limit: 512 megabytes 唐纳德先生的某女性朋友最近与唐纳德先生同居.该女性朋友携带一 baby.该 b ...
- swiper轮播 swiper整屏轮播
近期坐了几个移动端 整屏轮播的 效果 之前都是自己一个个写,之前听说过swiper插件,没有使用过,今天一尝试,果然,爽 使用方法示例 <div class="swiper-cont ...
- xcopy语法
xcopy语法 2007-02-09 13:29:45| 分类: 服务器 | 标签:xcopy语法 |字号 订阅复制文件和目录,包括子目录. 语法 xcopySource [Destination] ...
- ACM学习历程—HDU5423 Rikka with Tree(搜索)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- Centos6.5命令行快捷键
ctrl+a打开一个新的终端 ctrl+l 清除屏幕内容 ctrl+a 切换到命令行开始ctrl+e 切换到命令行末尾ctrl+u 剪切光标之前的内容ctrl+k 剪切光标之后的内容 Ctrl+-&g ...
- Poj_1045
这道题难点在于基本物理知识和数学的结合. 得出公式后再code,那就是小菜一碟了. import java.util.Scanner; import java.lang.Math; public cl ...
- translate 实现元素垂直居中
<div class="demo2"> <span>about me</span> </div> css .demo2{ width ...
- Lua虚拟机初始化
转自:http://www.cnblogs.com/ringofthec/archive/2010/11/09/lua_State.html 1. 创建lua虚拟机 lua_State *lua_ne ...
- 通过pip3安装ipython
操作系统:Centos7.4:ipython可以用来测试python语句,故需要安装. 首先需要安装epelde的扩展库: easy_install是由PEAK(Python Enterprise A ...