Python高级特性(2):Closures、Decorators和functools(转)
原文:Python高级特性(2):Closures、Decorators和functools
装饰器(Decorators)
装饰器是这样一种设计模式:如果一个类希望添加其他类的一些功能,而不希望通过继承或是直接修改源代码实现,那么可以使用装饰器模式。简单来说 Python中的装饰器就是指某些函数或其他可调用对象,以函数或类作为可选输入参数,然后返回函数或类的形式。通过这个在Python2.6版本中被新 加入的特性可以用来实现装饰器设计模式。
顺便提一句,在继续阅读之前,如果你对Python中的闭包(Closure)概念不清楚,请查看本文结尾后的附录,如果没有闭包的相关概念,很难恰当的理解Python中的装饰器。
在Python中,装饰器被用于用@语法糖修辞的函数或类。现在让我们用一个简单的装饰器例子来演示如何做一个函数调用日志记录器。在这个例子中, 装饰器将时间格式作为输入参数,在调用被这个装饰器装饰的函数时打印出函数调用的时间。这个装饰器当你需要手动比较两个不同算法或实现的效率时很有用。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
def logged(time_format): def decorator(func): def decorated_func(*args, **kwargs): print "- Running '%s' on %s " % (func.__name__,time.strftime(time_format)) start_time = time.time() result = func(*args, **kwargs) end_time = time.time() print "- Finished '%s', execution time = %0.3fs " % (func.__name__,end_time - start_time) return result decorated_func.__name__ = func.__name__ return decorated_func return decorator |
来看一个例子,在这里add1和add2函数被logged修饰,下面给出了一个输出示例。请注意在这里时间格式参数是存储在被返回的装饰器函数中 (decorated_func)。这就是为什么理解闭包对于理解装饰器来说很重要的原因。同样也请注意返回函数的名字是如何被替换为原函数名的,以防万 一如果它还要被使用到,这是为了防止混淆。Python默认可不会这么做。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@logged("%b %d %Y - %H:%M:%S")def add1(x, y): time.sleep(1) return x + y@logged("%b %d %Y - %H:%M:%S")def add2(x, y): time.sleep(2) return x + yprint add1(1, 2)print add2(1, 2)# Output:- Running 'add1' on Jul 24 2013 - 13:40:47- Finished 'add1', execution time = 1.001s3- Running 'add2' on Jul 24 2013 - 13:40:48- Finished 'add2', execution time = 2.001s3 |
如果你足够细心,你可能会注意到我们对于返回函数的名字__name__有着特别的处理,但对其他的注入__doc__或是__module__则 没有如此。所以如果,在这个例子中add函数有一个doc字符串的话,它就会被丢弃。那么该如何处理呢?我们当然可以像处理__name__那样对待所有 的字段,不过如果在每个装饰器内都这么做的话未免太繁冗了。这就是为何functools模块提供了一个名为wraps的装饰器的原因,那正是为了处理这 种情况。可能在理解装饰器的过程中会被迷惑,不过当你把装饰器看成是一个接收函数名作为输入参数并且返回一个函数,这样就很好理解了。我们将在下个例子中 使用wraps装饰器而不是手动去处理__name__或其他属性。
下个例子会有点复杂,我们的任务是将一个函数调用的返回结果缓存一段时间,输入参数决定缓存时间。传递给函数的输入参数必须是可哈希的对象,因为我 们使用包含调用输入参数的tuple作为第一个参数,第二个参数则为一个frozenset对象,它包含了关键词项kwargs,并且作为cache key。每个函数都会有一个唯一的cache字典存储在函数的闭包内。
【译注】set和frozenset为Python的两种内建集合,其中前者为可变 对象(mutable),其元素可以使用add()或remove()进行变更,而后者为不可变对象(imutable)并且是可哈希的 (hashable),在建立之后元素不可变,他可以作为字典的key或是另一个集合的元素。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import timefrom functools import wrapsdef cached(timeout, logged=False): """Decorator to cache the result of a function call. Cache expires after timeout seconds. """ def decorator(func): if logged: print "-- Initializing cache for", func.__name__ cache = {} @wraps(func) def decorated_function(*args, **kwargs): if logged: print "-- Called function", func.__name__ key = (args, frozenset(kwargs.items())) result = None if key in cache: if logged: print "-- Cache hit for", func.__name__, key (cache_hit, expiry) = cache[key] if time.time() - expiry < timeout: result = cache_hit elif logged: print "-- Cache expired for", func.__name__, key elif logged: print "-- Cache miss for", func.__name__, key # No cache hit, or expired if result is None: result = func(*args, **kwargs) cache[key] = (result, time.time()) return result return decorated_function return decorator |
来看看它的用法。我们使用装饰器装饰一个很基本的斐波拉契数生成器。这个cache装饰器将对代码使用备忘录模式(Memoize Pattern)。请注意fib函数的闭包是如何存放cache字典、一个指向原fib函数的引用、logged参数的值以及timeout参数的最后值 的。dump_closure将在文末定义。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
>>> @cached(10, True)... def fib(n):... """Returns the n'th Fibonacci number."""... if n == 0 or n == 1:... return 1... return fib(n - 1) + fib(n - 2)...-- Initializing cache for fib>>> dump_closure(fib)1. Dumping function closure for fib:-- cell 0 = {}-- cell 1 =-- cell 2 = True-- cell 3 = 10>>>>>> print "Testing - F(4) = %d" % fib(4)-- Called function fib-- Cache miss for fib ((4,), frozenset([]))-- Called function fib-- Cache miss for fib ((3,), frozenset([]))-- Called function fib-- Cache miss for fib ((2,), frozenset([]))-- Called function fib-- Cache miss for fib ((1,), frozenset([]))-- Called function fib-- Cache miss for fib ((0,), frozenset([]))-- Called function fib-- Cache hit for fib ((1,), frozenset([]))-- Called function fib-- Cache hit for fib ((2,), frozenset([]))Testing - F(4) = 5 |
Class Decorators
在之前的小节中,我们看了一些函数装饰器和一些使用的小技巧,接下来我们来看看类装饰器。类装饰器将一个class作为输入参数(Python中的一种类类型对象),并且返回一个修改过的class。
第一个例子是一个简单的数学问题。当给定一个有序集合P,我们定义Pd为P的反序集合P(x,y) <-> Pd(x,y),也就是说两个有序集合的元素顺序互为相反的,这在Python中该如何实现?假定一个类定义了__lt__以及__le__或其他方法来实现有序。那么我们可以通过写一个类装饰器来替换这些方法。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
def make_dual(relation): @wraps(relation, ['__name__', '__doc__']) def dual(x, y): return relation(y, x) return dualdef dual_ordering(cls): """Class decorator that reverses all the orderings""" for func in ['__lt__', '__gt__', '__ge__', '__le__']: if hasattr(cls, func): setattr(cls, func, make_dual(getattr(cls, func))) return cls |
下面是将这个装饰器用以str类型的例子,创建一个名为rstr的新类,使用反字典序(opposite lexicographic)为其顺序。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@dual_orderingclass rstr(str): passx = rstr("1")y = rstr("2")print x < yprint x <= yprint x > yprint x >= y# Output:FalseFalseTrueTrue |
来看一个更复杂的例子。假定我们希望前面所说的logged装饰器能够被用于某个类的所有方法。一个方案是在每个类方法上都加上装饰器。另一个方案 是写一个类装饰器自动完成这些工作。在动手之前,我将把前例中的logged装饰器拿出来做一些小改进。首先,它使用functools提供的wraps 装饰器完成固定__name__的工作。第二,一个_logged_decorator属性被引入(设置为True的布尔型变量),用来指示这个方法是否 已经被装饰器装饰过,因为这个类可能会被继承而子类也许会继续使用装饰器。最后,name_prefix参数被加入用来设置打印的日志信息。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
def logged(time_format, name_prefix=""): def decorator(func): if hasattr(func, '_logged_decorator') and func._logged_decorator: return func @wraps(func) def decorated_func(*args, **kwargs): start_time = time.time() print "- Running '%s' on %s " % (name_prefix + func.__name__,time.strftime(time_format)) result = func(*args, **kwargs) end_time = time.time() print "- Finished '%s', execution time = %0.3fs " % (name_prefix + func.__name__,end_time - start_time) return result decorated_func._logged_decorator = True return decorated_func return decorator |
好的,让我们开始写类装饰器:
|
1
2
3
4
5
6
7
8
9
10
11
|
def log_method_calls(time_format): def decorator(cls): for o in dir(cls): if o.startswith('__'): continue a = getattr(cls, o) if hasattr(a, '__call__'): decorated_a = logged(time_format, cls.__name__ + ".")(a) setattr(cls, o, decorated_a) return cls return decorator |
下面是使用方法,注意被继承的或被重写的方法是如何处理的。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@log_method_calls("%b %d %Y - %H:%M:%S")class A(object): def test1(self): print "test1"@log_method_calls("%b %d %Y - %H:%M:%S")class B(A): def test1(self): super(B, self).test1() print "child test1" def test2(self): print "test2"b = B()b.test1()b.test2()# Output:- Running 'B.test1' on Jul 24 2013 - 14:15:03- Running 'A.test1' on Jul 24 2013 - 14:15:03test1- Finished 'A.test1', execution time = 0.000schild test1- Finished 'B.test1', execution time = 1.001s- Running 'B.test2' on Jul 24 2013 - 14:15:04test2- Finished 'B.test2', execution time = 2.001s |
我们第一个类装饰器的例子是类的反序方法。一个相似的装饰器,可以说是相当有用的,实现__lt__、__le__、__gt__、__ge__和 __eq__中的一个,能够实现类的全排序么?这也就是functools.total_ordering装饰器所做的工作。详情请见参考文档。
Flask中的一些例子
让我们来看看Flask中用到的一些有趣的装饰器。
假定你希望让某些函数在特定的调用时刻输出警告信息,例如仅仅在debug模式下。而你又不希望每个函数都加入控制的代码,那么你就能够使用装饰器来实现。以下就是Flask的app.py中定义的装饰器的工作。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def setupmethod(f): """Wraps a method so that it performs a check in debug mode if the first request was already handled. """ def wrapper_func(self, *args, **kwargs): if self.debug and self._got_first_request: raise AssertionError('A setup function was called after the ' 'first request was handled. This usually indicates a bug ' 'in the application where a module was not imported ' 'and decorators or other functionality was called too late.\n' 'To fix this make sure to import all your view modules, ' 'database models and everything related at a central place ' 'before the application starts serving requests.') return f(self, *args, **kwargs) return update_wrapper(wrapper_func, f) |
来看一个更有趣的例子,这个例子是Flask的route装饰器,在Flask类中定义。注意到装饰器可以是类中的一个方法,将self作为第一个 参数。完整的代码在app.py中。请注意装饰器简单的将被装饰过的函数注册成为一个URL句柄,这是通过调用add_url_rule函数来实现的。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
def route(self, rule, **options): """A decorator that is used to register a view function for a given URL rule. This does the same thing as :meth:`add_url_rule` but is intended for decorator usage:: @app.route('/') def index(): return 'Hello World' For more information refer to :ref:`url-route-registrations`. :param rule: the URL rule as string :param endpoint: the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint :param options: the options to be forwarded to the underlying :class:`~werkzeug.routing.Rule` object. A change to Werkzeug is handling of method options. methods is a list of methods this rule should be limited to (`GET`, `POST` etc.). By default a rule just listens for `GET` (and implicitly `HEAD`). Starting with Flask 0.6, `OPTIONS` is implicitly added and handled by the standard request handling. """ def decorator(f): endpoint = options.pop('endpoint', None) self.add_url_rule(rule, endpoint, f, **options) return f return decorator |
扩展阅读
2. metaprogramming in Python 3
附录:闭包
一个函数闭包是一个函数和一个引用集合的组合,这个引用集合指向这个函数被定义的作用域的变量。后者通常指向一个引用环境(referencing environment),这使得函数能够在它被定义的区域之外执行。在Python中,这个引用环境被存储在一个cell的tuple中。你能够通过 func_closure或Python 3中的__closure__属性访问它。要铭记的一点是引用及是引用,而不是对象的深度拷贝。当然了,对于不可变对象而言,这并不是问题,然而对可变对 象(list)这点就必须注意,随后会有一个例子说明。请注意函数在定义的地方也有__globals__字段来存储全局引用环境。
来看一个简单的例子:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
>>> def return_func_that_prints_s(s):... def f():... print s... return f...>>> g = return_func_that_prints_s("Hello")>>> h = return_func_that_prints_s("World")>>> g()Hello>>> h()World>>> g is hFalse>>> h.__closure__(,)>>> print [str(c.cell_contents) for c in g.__closure__]['Hello']>>> print [str(c.cell_contents) for c in h.__closure__]['World'] |
一个稍复杂的例子。确保明白为什么会这么执行。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> def return_func_that_prints_list(z):... def f():... print z... return f...>>> z = [1, 2]>>> g = return_func_that_prints_list(z)>>> g()[1, 2]>>> z.append(3)>>> g()[1, 2, 3]>>> z = [1]>>> g()[1, 2, 3] |
【译者】:z.append(3)时,g()内部的引用和z仍然指向一个变量,而z=[1]之后,两者就不再指向一个变量了。
最后,来看看代码中使用到的dump_closure方法的定义。
|
1
2
3
4
5
6
7
|
def dump_closure(f): if hasattr(f, "__closure__") and f.__closure__ is not None: print "- Dumping function closure for %s:" % f.__name__ for i, c in enumerate(f.__closure__): print "-- cell %d = %s" % (i, c.cell_contents) else: print " - %s has no closure!" % f.__name__ |
Python高级特性(2):Closures、Decorators和functools(转)的更多相关文章
- Python高级特性(3): Classes和Metaclasses(转)
原文:Python高级特性(3): Classes和Metaclasses 类和对象 类和函数一样都是Python中的对象.当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变 ...
- 三、python高级特性(切片、迭代、列表生成器、生成器)
1.python高级特性 1.1切片 list列表 L=['Mli','add','sal','saoo','Lkkl'] L[0:3] #即为['Mli','add','sal'] 从索引0开始 ...
- python高级特性:切片/迭代/列表生成式/生成器
廖雪峰老师的教程上学来的,地址:python高级特性 下面以几个具体示例演示用法: 一.切片 1.1 利用切片实现trim def trim(s): while s[:1] == " &qu ...
- python高级特性和高阶函数
python高级特性 1.集合的推导式 列表推导式,使用一句表达式构造一个新列表,可包含过滤.转换等操作. 语法:[exp for item in collection if codition] if ...
- Python高级特性(1):Iterators、Generators和itertools(转)
译文:Python高级特性(1):Iterators.Generators和itertools [译注]:作为一门动态脚本语言,Python 对编程初学者而言很友好,丰富的第三方库能够给使用者带来很大 ...
- Python高级特性之:List Comprehensions、Generator、Dictionary and set ...
今天帅气的易哥和大家分享的是Pyton的高级特性,希望大家能和我一起学习这门语言的魅力. Python高级特性之:List Comprehensions.Generator.Dictionary an ...
- Python 高级特性介绍 - 迭代的99种姿势 与协程
Python 高级特性介绍 - 迭代的99种姿势 与协程 引言 写这个笔记记录一下一点点收获 测试环境版本: Python 3.7.4 (default, Sep 28 2019, 16:39:19) ...
- Python高级特性(切片,迭代,列表生成式,生成器,迭代器)
掌握了Python的数据类型.语句和函数,基本上就可以编写出很多有用的程序了. 比如构造一个1, 3, 5, 7, ..., 99的列表,可以通过循环实现: L = [] n = 1 while n ...
- Python高级特性: 函数编程 lambda, filter,map,reduce
一.概述 Python是一门多范式的编程语言,它同时支持过程式.面向对象和函数式的编程范式.因此,在Python中提供了很多符合 函数式编程 风格的特性和工具. 以下是对 Python中的函数式编程 ...
随机推荐
- mysql 位运算
& 与运算 | 或运算 ^ 异或运算 或者 你也可以将 与运算理解为 + 法 例如 1|2 = 3 (1+2 = 3)1|2|4 = 7 (1+2+4 = 7) 将 异或运算理解为 - ...
- swift侧开菜单
此文来自学习这篇博客后的学习笔记,原博客是用oc写的,我最近在学swift,于是改写为swift. swift和oc之间互相调用还是很方便的,但是要注意AnyObject和optional的运用,我现 ...
- 知乎日报 API 分析
声明 下面全部 API 均由 知乎(Zhihu.Inc) 提供,本人採取非正常手段获取. 获取与共享之行为或有侵犯知乎权益的嫌疑.若被告知需停止共享与使用.本人会及时删除此页面与整个项目. 请您暸解相 ...
- mysql client中使用帮助命令
当前MySQL服务器的版本号 使用那个命令来参看MySQL的帮助信息 帮助主题供我们查看. 命令为: ? contents 例如查看max方法的使用方法则输入? max即可 这个在navcat中是不支 ...
- Android——onCreate( )方法详解(转)
android开发之onCreate( )方法详解 onCreate( )方法是android应用程序中最常见的方法之一,那么,我们在使用onCreate()方法的时候应该注意哪些问题呢? 先看看Go ...
- KMP算法完整教程 (上)
KMP算法完整教程 全称: Knuth_Morris_Pratt Algorithm(KMP算法) 类型: 高级检索算法 功能: 字符串匹配查找 提出者: D.E.Knuth(克努兹),J.H.Mor ...
- IOS 中微信 网页授权报 key[也就是code]失效 解决办法
枪魂微信平台ios手机点击返回 网页授权失败,报key失效.已经解决,原因是授权key只能使用一次,再次使用就会失效. 解决办法:第一次从菜单中进行授权时,用session记录key和open_id. ...
- 【POJ】1094 Sorting It All Out(拓扑排序)
http://poj.org/problem?id=1094 原来拓扑序可以这样做,原来一直sb的用白书上说的dfs............ 拓扑序只要每次将入度为0的点加入栈,然后每次拓展维护入度即 ...
- media wiki run on nginx
1. 环境安装: nginx安装 nginx-1.5.7 php安装 PHP 5.4.10 (cli) (built: Jul 30 2014 16:45:08) mysql安装 Ver 14.14 ...
- Centos下使用压缩包安装MySQL5.7
今天在自己的centos服务器上安装mysql,碰到的问题相当的多,装个mysql远比在windows复杂的多.这里通过查找的一些博文(包括前几篇)来记录安装mysql时的各种问题.可能步骤不完整,当 ...