Python函数小结(2)-- 装饰器、 lambda
本篇依然是一篇学习笔记,文章的结构首先讲装饰器,然后讲lambda表达式。装饰器内容较多,先简要介绍了装饰器语法,之后详细介绍理解和使用不带参数装饰器时应当注意到的一些细节,然后实现了一个简单的缓存装饰器作为实践,最后在理解不带参数的装饰器的基础上,介绍了理解和使用带参数的装饰器需要注意到的细节。lambda表达式的讲解依然着重于使用细节和理解上。如果有不对的地方欢迎指正。
装饰器
实际上理解装饰器的作用很简单,在看core python相关章节的时候大概就是这种感觉。只是在实际应用的时候,发现自己很难靠直觉决定如何使用装饰器,特别是带参数的装饰器,于是摊开来思考了一番,写下一些心得。
装饰器简述
为了完整起见,这里简要说明一下装饰器的语法。装饰器分为带参数得装饰器以及不带参数得装饰器。装饰器以及使用效果看起来大概是这样的。
#语法是这个样子的
@decorator(dec_opt_args)
def func2Bdecorated(func_opt_args):
	...
#不带参数的装饰器
@dec1
@dec2
def func():
	...
#这个函数声明等价于
func = dec1(dec2(func))
#带参数的装饰器
@dec(some_args)
def func():
	...
#这个函数声明等价于
func = dec(some_args)(func)
不带参数的装饰器需要注意的一些细节
这里将说明使用带参数的装饰器时需要注意的一些细节。并会实现一个简单缓存装饰器,来帮助理解。
1. 关于装饰器函数(decorator)本身
对于被装饰的函数func,不带参数的装饰器接受一个函数为参数,并返回一个装饰过的函数decorated_func。因为对返回的函数没有限制,所以decorator函数甚至可以返回与func完全无关的新函数。但是大部分情况下,decorated_func是对func的额外处理,因此一个装饰器一般对应两个函数,一个是decorator函数,用来进行一些初始化操作处理,一个是decorated_func用来实现对被装饰的函数func的额外处理。并且为了保持对func的引用,decorated_func一般作为decorator的内部函数,比如:
#一般将decorated_func作为decorator的内部函数
#因为内部函数可以保持对func的引用(详见(1)的闭包讲解)
>>> def decorator(func):
...     print 'init opration'
...     def decorated_func():
...             return func(2)
...     return decorated_func
... 
2. decorator函数只在函数声明的时候被调用一次
装饰器实际上是语法糖,在声明函数之后就会被调用,产生decorated_func,并把func符号的引用替换为decorated_func。之后每次调用func函数,实际调用的是decorated_func。
>>> def decorator(func):
...     def decorated_func():
...         func(1)
...     return decorated_func
...
#声明时就被调用
>>> @decorator
... def func(x):
...     print x
...
decorator being called
#使用func()函数实际上使用的是decorated_func函数
>>> func()
1
>>> func.__name__
'decorated_func'
如果要保证返回的decorated_func的函数名与func的函数名相同,应当在decorator函数返回decorated_func之前,加入decorated_func.name = func.name, 另外functools模块提供了wraps装饰器,可以完成这一动作。
#@wraps(func)的操作相当于
#在return decorated_func之前,执行
#decorated_func.__name__ = func.__name__
#func作为装饰器参数传入,
#decorated_func则作为wraps返回的函数的参数传入
>>> def decorator(func):
...		@wraps(func)
...     def decorated_func():
...         func(1)
...     return decorated_func
...
#声明时就被调用
>>> @decorator
... def func(x):
...     print x
...
decorator being called
#使用func()函数实际上使用的是decorated_func函数
>>> func()
1
>>> func.__name__
'func'
3. decorator函数局部变量的妙用
因为closure的特性(详见(1)部分闭包部分的详解),decorator声明的变量会被decorated_func.func_closure引用,所以调用了decorator方法结束之后,decorator方法的局部变量也不会被回收,因此可以用decorator方法的局部变量作为计数器,缓存等等。值得注意的是,如果要改变变量的值,该变量一定要是可变对象,因此就算是计数器,也应当用列表来实现。并且声明一次函数调用一次decorator函数,所以不同函数的计数器之间互不冲突,例如:
#!/usr/bin/env python
#filename decorator.py
def decorator(func):
	#注意这里使用可变对象
    a = [0]
    def decorated_func(*args,**keyargs):
        func(*args, **keyargs)
        #因为闭包是浅拷贝,如果是不可变对象,每次调用完成后符号都会被清空,导致错误
        a[0] += 1
		print "%s have bing called %d times" % (func.__name__, a[0])
    return decorated_func
@decorator
def func(x):
    print x
@decorator
def theOtherFunc(x):
    print x
>>> from decorator import func
>>> from decorator import theOtherFunc
>>> func(0)
0
func have bing called 1 times
>>> func(0)
0
func have bing called 2 times
>>> func(0)
0
func have bing called 3 times
>>> theOtherFunc(0)
0
theOtherFunc have bing called 1 times
>>> theOtherFunc(1)
1
theOtherFunc have bing called 2 times
>>> theOtherFunc(2)
2
theOtherFunc have bing called 3 times
4. 简单的结果缓存装饰器
#coding=UTF-8
#!/usr/bin/env python
#filename decorator.py
import time
from functools import wraps
def decorator(func):
	"cache for function result, which is immutable with fixed arguments"
    print "initial cache for %s" % func.__name__
    cache = {}
    @wraps(func)
    def decorated_func(*args,**kwargs):
        #key必须是可哈希对象
        #这里其实不严谨,如果kwargs值有不可哈希对象会出错
        #简单起见这里不再做特殊处理
        key = (args, tuple(kwargs.items()))
        result = None
        #判断是否存在缓存
        if key in cache:
            (result, updateTime) = cache[key]
            #过期时间固定为10秒
            if time.time() -updateTime < 10:
                print "cache hit for", key
            else :
                print  "cache expired for", key
                result = None
        else:
            print "no cache for ", key
        #如果过期,或则没有缓存调用方法
        if result is None:
            result = func(*args, **kwargs)
        cache[key] = (result, time.time())
        return result
    return decorated_func
@decorator
def func(x):
    if x <=1:
        return 1
    return x + func(x-1)
>>> from decorator import func
initial cache for func
>>> func(5)
no cache for  ((5,), ())
no cache for  ((4,), ())
no cache for  ((3,), ())
no cache for  ((2,), ())
no cache for  ((1,), ())
15
>>> func(5)
cache hit for ((5,), ())
15
>>> func(1)
cache expired for ((1,), ())
1
>>> func(2)
cache expired for ((2,), ())
cache hit for ((1,), ())
3
带参数的装饰器
  熟悉了不带参数的装饰器的使用之后,理解带参数的装饰器就简单很多了。带参数的装饰器主要用来传递一些设置,或者用来选择不同的装饰器。
  我们已经知道,不带参数的装饰器调用decorator返回decorated_func。那么带参数的装饰器,就是返回decorator方法,再由decorator方法处理后,返回decorated_func。因此带参数的装饰器一般由3个方法组成,首先,调用settings_func用来接受参数, 并选择decorator方法, 之后调用返回的decorator方法产生decorated_func来对func进行额外处理。
  有了settings_func我们就可以对decorator进行定制。如之前的实现的缓存方法,过期时间固定为10秒,有了settings_func我们就可以自定义过期时间,判断是否进行调试输出等。
1. 为缓存装饰器增加配置参数
  这里加入了对过期时间的配置,和调试输出的开关。
  
#coding=UTF-8
#!/usr/bin/env python
#filename decorator.py
import time
from functools import wraps
def cache(expirationTime, debug=False):
    def decorator(func):
        if debug:
            print "initial cache for %s" % func.__name__
        cache = {}
        @wraps(func)
        def decorated_func(*args,**kwargs):
            #key必须是可哈希对象
            #这里其实不严谨,如果kwargs值有不可哈希对象会出错
            #简单起见这里不再做特殊处理
            key = (args, tuple(kwargs.items()))
            result = None
            if key in cache:
                (result, updateTime) = cache[key]
                if time.time() -updateTime < expirationTime:
                    print "cache hit for", key
                else :
                    if debug:
                        print  "cache expired for", key
                    result = None
            elif debug:
                print "no cache for ", key
            if result is None:
                result = func(*args, **kwargs)
            cache[key] = (result, time.time())
            return result
        return decorated_func
    return decorator
@cache(10)
def func(x):
    if x <=1:
	return 1
    return x + func(x-1)
可以看到除了cache hit,其他的消息都被debug=False关闭了。
>>> func(5)
15
>>> func(1)
cache hit for ((1,), ())
1
>>> func(2)
cache hit for ((2,), ())
3
>>> func(3)
cache hit for ((2,), ())
6
被装饰的函数共用变量
上面的例子由于装饰器的变量(计数器,缓存)是在装饰器的方法中声明的,所以不同方法的这些变量是不通用的。要使得同一个装饰器装饰的不同方法变量通用(如共用缓存等),可以使用类属性,或全局变量来实现。
lambda表达式
lambda表达式实际上就是匿名函数,类似javascript的function([arg1,[arg2[...]]]){...}。 python中lambda表达式返回的就是函数实例。 语法中lambda后面跟随的是参数, 冒号后面跟随的是返回的结果。
>>> bar = lambda x,y : x+y
>>> type(bar)
<type 'function'>
>>> bar(1,2)
3
同时,lambda表达式也具有closure(闭包)的特性:
>>> def foo():
...     x = 5
...     y = 5
...     bar = lambda : x+y
...     return bar
...
>>> foo()()
10
总之,函数具有的特性,lambda表达式都具有。像给参数赋默认值啊, lambda内部的变量不受外部影响啊,全部都与函数的行为一模一样。
#默认参数
>>> y=2
>>> bar = lambda x, y =y : x + y
>>> bar(3)
5
#lambda定义的变量不受外部影响
>>> y = 5
>>> bar(3)
5
>>> 
												
											Python函数小结(2)-- 装饰器、 lambda的更多相关文章
- python函数与方法装饰器
		
之前用python简单写了一下斐波那契数列的递归实现(如下),发现运行速度很慢. def fib_direct(n): assert n > 0, 'invalid n' if n < 3 ...
 - Python函数篇:装饰器
		
装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理. ...
 - python函数学习之装饰器
		
装饰器 装饰器的本质是一个python函数,它的作用是在不对原函数做任何修改的同时,给函数添加一定的功能.装饰器的返回值也是一个函数对象. 分类: 1.不带参数的装饰器函数: def wrapper( ...
 - python函数:叠加装饰器、迭代器、自定义迭代器、生成式
		
一.叠加多个装饰器二.迭代器三.自定义迭代器四.xxx生成式 一.叠加多个装饰器 # 加载装饰器就是将原函数名偷梁换柱成了装饰器最内层那个wrapper函数 # 在加载完毕后,调用原函数其实就是在调用 ...
 - python 函数基础及装饰器
		
没有参数的函数及return操作 def test1(): print ("welcome") def test2(): print ("welcomt test2&qu ...
 - python学习笔记(五):装饰器、生成器、内置函数、json
		
一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如说,我以前写了很多代码,系统已经上线了,但是性能比较不好,现在想把程序里 ...
 - python笔记5:装饰器、内置函数、json
		
装饰器 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 先看简单例子: def run(): time.sleep(1 ...
 - Python学习日记(九) 装饰器函数
		
1.import time a.time.time() 获取到当前的时间,返回值为浮点型 import time print(time.time()) #1565422783.6497557 b.ti ...
 - python基础之函数当中的装饰器
		
在实际工作当中存在一个开放封闭原则 1.对扩展是开放的 为什么要对扩展开放呢? 我们说,任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代码扩展.添加新 ...
 - 第四天 内置函数2 随机码 装饰器 迭代器、生成器 递归 冒泡算法 JSON
		
关于函数的return li = [11,22,33,44] def f1(arg): arg.append(55) li = f1(li) print(li) 因为li = f1(li) 实际赋值的 ...
 
随机推荐
- Android调用系统的Activity、ContentProvider、Service、Broadcast Receiver
			
Intent-------->Action |_____________Activity------------------------------->Intent Action |___ ...
 - android and javascript
			
layout-------->HTML activity-------->JavaScript R资源管理者----------->layout, string, styles, c ...
 - LeetCode_Minimum Window Substring
			
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
 - Altium designer快捷键
			
1. 先设置参数,开启高亮显示,见下图红圈处: (1)选择使能可以高亮:CTRL+鼠标左键点击相应PCB网络即可高亮 (2)选择仅切换键时高亮显示,可以在CTRL+鼠标左键点击相应PCB网络高亮后,移 ...
 - B树叶子节点split
			
一.B-Tree索引的分裂 1. 创建测试表 SQL> create table split_tab (id number, name varchar2(100)); 表已创建. SQL> ...
 - c#发送邮件样例
			
1.通过gmail邮箱发送邮件 try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(& ...
 - sqlite3 C API编程
			
1,创建测试数据库,表;
 - 一、mysql分表简单介绍
			
一.Mysql分表的原因 1.当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了. 分表的目的就在于此,减小数据库的负担,缩短查询时间. 2.mysql中 ...
 - Hadoop 1、在虚拟机上进行 HDFS 安装
			
一.准备条件 1.四台Linux虚拟机(1台NameNode节点,1台Secondary节点(Secondary和其中1台DataNode共用),外加2台DataNode) 2.下载Hadoop版本, ...
 - python glob标准库基础学习
			
#glob文件名模式匹配#作用:使用unix shell规则查找与一个模式匹配文件名"""尽管glob api很小,但这个模块很强大,只要程序需要查找文件系统中名字与某种 ...