装饰器

2017-07-20  本文已影响0人  tf_dejs

装饰器

函数装饰器,将其他函数功能增强,实现函数代码重用,函数功能重用。性能测试,插日志,抽离出大量和函数无关的

装饰器:

装饰器一般接受一个函数对象作为参数,以对其进行增强

def deco(func):
    def wrapper():
        print('please say something:')
        func()
        print('no zuo no die')
    return wrapper

@deco
def show():
    print('hello')

show()


闭包

函数对象(变量名为函数名)
    函数体

函数的参数:位置参数,关键字参数,argv位置参数,*kwargv关键字参数,参数分解赋值

函数可以嵌套,在函数中定义并调用,在函数中定义函数,并将其返回就叫做闭合函数,也就是闭包
功用:内层函数记忆外?层函数调用的变量

In [12]: def f(x):
   ....:     def f2(y):
   ....:         return x ** y
   ....:     return f2
   ....: 

In [13]: f(3)
Out[13]: <function __main__.f2>

In [14]: type(f(3))     # f(3) 是一个函数
Out[14]: function

In [15]: ff = f(3)

In [16]: ff(4)    
Out[16]: 81

In [17]: ff(5)
Out[17]: 243

In [18]: type(ff(4))       # ff(4)是一个整数
Out[18]: int


一切皆对象

In [27]: def hi():
   ....:     print('hello')
   ....:     

In [28]: hi()
hello

In [29]: greet = hi     # 函数名不过是函数体的个变量名,在不需要调用时,可直接作为变量拿来用

In [30]: greet()
hello

In [31]: del hi

In [32]: hi()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-32-9c04baa18e05> in <module>()
----> 1 hi()

NameError: name 'hi' is not defined

In [33]: greet()
hello

在函数中定义函数

从函数中返回函数

将函数作为参数传递给函数

蓝本规范:

    from functools import wraps
    def decorator_name(f):
        @wraps(f)
        def decorated(*args, **kwargs):
            if not can_run:
                return "Function will not run"
            return f(*args, **kwargs)
        return decorated

    @decorator_name
    def func():
        return("Function is running")

    can_run = True
    print(func())
    # Output: Function is running

    can_run = False
    print(func())
    # Output: Function will not run

注意:@wraps接受一个函数来进行装饰,并加入了复制函数名称、注释文档、参数列表等等的功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性。

使用场景

授权

装饰器能有助于检查某个人是否被授权去使用一个web应用的端点(endpoint)。它们被大量使用于Flask和Django web框架中。这里是一个例子来使用基于装饰器的授权:

from functools import wraps

def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            authenticate()
        return f(*args, **kwargs)
    return decorated

日志

日志是装饰器运用的另一个亮点。这是个例子:

from functools import wraps

def logit(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print(func.__name__ + " was called")
        return func(*args, **kwargs)
    return with_logging

@logit
def addition_func(x):
   """Do some math."""
   return x + x


result = addition_func(4)
# Output: addition_func was called

带参数的装饰器

在函数中嵌入装饰器
装饰器类

参考:

马哥python
python进阶


暂时只理解到简单的装饰器

上一篇 下一篇

猜你喜欢

热点阅读