谈谈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

后记

先介绍到这里,大致也对修饰器有了一定的理解。后面自己会结合自己的项目继续深入学习。

原文地址:https://www.cnblogs.com/yunlambert/p/9501294.html

时间: 2024-08-02 04:46:15

谈谈python修饰器的相关文章

PYTHON修饰器的函数式编程

转自:http://coolshell.cn/articles/11265.html Python修饰器的函数式编程 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都很相似--都是想要对一个已有的模块做一些"修饰工作",所谓修饰工作就是想给现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能

Python修饰器

Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都很相似——都是想要对一个已有的模块做一些“修饰工作”,所谓修饰工作就是想给现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能)侵入到原有的模块中的代码里去.但是OO的Decorator简直就是一场恶梦,不信你就去看看wikipedia上的词条(Deco

python 修饰器 最好的讲解

Python的修饰器的英文名叫Decorator,修饰器就是对一个已有的模块做一些"修饰工作",比如在现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能)侵入到原有的模块中的代码里去. Python 的 Decorator在使用上和Java/C#的Annotation很相似,就是在方法名前面加一个@XXX注解来为这个方法装饰一些东西.但是,Java/C#的Annotation也很让人望而却步,太TMD的复杂了,你要玩它,你需要了解一堆An

python 修饰器

因困扰自己多时,打算整理一下修饰器到底是什么? 参考资料:http://python.jobbole.com/82344/ 修饰器 1. 定义2个函数 2. 基本实现 3. 问题:实现后, 要修改原来代码中的变量名, funcB()改为funcA(funcB) 4. 解决方法:让funcB赋值等于funcA(funcB),用时就是funcB(), 不用修改原来代码, 这个要求需funcA(funcB)返回的是一个函数     a) 所以要返回一个函数,则加一个函数 wrapper(),然后ret

Python修饰器学习总结

先贴上一个很清晰的分步学帖子 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- coding:gbk -*- '''示例1: 最简单的函数,表示调用了两次''' def myfunc():     print("myfunc() called.") myfunc() myfunc() 第二步:使用装饰函数在函数执行前和执行后分别附加额外功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # -*- codin

Python修饰器讲解

转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 文章先由stackoverflow上面的一个问题引起吧,如果使用如下的代码: @makebold @makeitalic def say(): return "Hello" 打印出如下的输出: <b><i>Hello<i></b> 你会怎么做?最后给出的答案是: def makebold(fn): de

深入浅出 Python 装饰器:16 步轻松搞定 Python 装饰器

Python的装饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都很相似--都是想要对一个已有的模块做一些"修饰工作",所谓修饰工作就是想给现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能)侵入到原有的模块中的代码里去.但是OO的Decorator简直就是一场恶梦,不信你就去看看wikipedia上的词条

Python的wraps修饰器详解

本文和大家分享的主要是python 中wraps 修饰器相关内容,一起来看看吧,希望对大家 学习python有所帮助. 在了解  wraps  修饰器之前,我们首先要了解  partial  和  update_wrapper  这两个函数,因为在 wraps  的代码中,用到了这两个函数. partial 首先说  partial  函数,在  官方文档  的描述中,这个函数的声明如下:  functools.partial(func, *args, **keywords)  .它的作用就是返

Python 从零学起(纯基础) 笔记 之 迭代器、生成器和修饰器

Python的迭代器. 生成器和修饰器 1. 迭代器是访问集合元素的一种方式,从第一个到最后,只许前进不许后退. 优点:不要求事先准备好整个迭代过程中的所有元素,仅仅在迭代到某个元素时才计算该元素,而在这之前或者之后,元素可以不存在或被销毁. 特点: 访问者是不需要关心迭代器内部的结构,仅需要通过next()方法不断去取下一个内容. 不能随机访问集合中的某个值,只能从头到尾依次访问. 访问到一半时不能往回退 便于循环比较大的数据集合,节省内存 1 names = iter(['alex','ja