24.一文理解Python函数装饰器

装饰器在 Python 使用如此方便都要归因于 Python 的函数能像普通的对象一样能作为参数传递给其他函数,可以被赋值给其他变量,可以作为返回值,可以被定义在另外一个函数内。

目录

0.前言

一、写出Python装饰器的步骤

1.一切皆对象

1.1Python中的函数

1.2函数赋值给变量

2.在函数中定义函数 创建嵌套函数

2.1注意🔗

3.从函数中返回函数

4.将函数作为参数传给另一个函数

4.1注意🔗:

5.第一个装饰器

5.1装饰器的一些常用场景

5.1.1蓝本规范

5.1.2注意🔗

5.2使用场景

5.2.1授权(Authorization)

5.3日志(Logging)

6.带参数的装饰器

6.1在函数中嵌入装饰器

6.2装饰器类

6.2.1添加email功能


@语法糖

@符号是装饰器的语法糖,它放在函数开始定义的地方,这样可以省略最后一步再次赋值的操作。

0.前言

装饰器(Decorators)是Python的一个重要部分;装饰器是修改其他函数的功能的函数,有助于让我们的代码更简洁。

一、写出Python装饰器的步骤

1.一切皆对象

1.1Python中的函数

def hi(name="passion"):
    return "hi " + name

print(hi())
# output: hi passion

1.2函数赋值给变量

我们甚至可以将一个函数赋值给一个变量,如下所示:(我们没有使用小括号,因为我们并不是在调用hi函数,而是在将它放在greet变量里面。)

def hi(name="passion"):
    return "hi " + name

print(hi())
# output: hi passion

greet = hi #没有使用小括号,是因为我们不是在调用hi()函数,而是将它赋值给变量greet!
print(greet())

#output =
# hi passion
# hi passion

删掉旧的hi函数,看看运行结果:

def hi(name="passion"):
    return "hi " + name

print(hi())
# output: hi passion

greet = hi
print(greet())

#output :
# hi passion
# hi passion

del hi
#print(hi())

#output:
# NameError : name 'hi' is not defined

print(greet())

#output:hi passion
# hi passion
# hi passion

2.在函数中定义函数 创建嵌套函数

在Python中,我们可以在一个函数中定义另一个函数:

def hi(name='Saturday'): #定义hi() function
    print("now you are inside the hi() function")

    def greet():#在hi() function 中定义了 greet() function
        return "now you are inside the greet() function"
     
    def welcome():#在hi() function 中定义了 welcome() function
        return "now you are inside the welcome() function"

    print(greet())
    print(welcome())
    print("now you are back in the hi() function")
 
hi()

2.1注意🔗

无论何时调用hi(),greet()和welcome()将会被同时调用,greet()和welcome()函数在hi()函数之外是不能访问的,如下所示:

def hi(name='Saturday'):
    print("now you are inside the hi() function")

    def greet():
        return "now you are inside the greet() function"
     
    def welcome():
        return "now you are inside the welcome() function"

    print(greet())
    print(welcome())
    print("now you are back in the hi() function")
 
#hi()

greet()

NameError: name 'greet' is not defined 

3.从函数中返回函数

其实并不需要在一个函数里去执行另一个函数,我们也可以将其作为输出返回出来:

def hi(name="enthusiasm"):
    def greet():
        return "now you are in the greet() function"

    def welcome():
        return "now you are in the welcome() function"

    if name == "enthusiasm":
        return greet
    else:
        return welcome
    

a = hi()
print(a)

output = <function hi.<locals>.greet at 0x106673700>

上面清晰地展示了"a"现在指向到hi()函数中的greet()函数 ,下面尝试print(a())

def hi(name="enthusiasm"):
    def greet():
        return "now you are in the greet() function"

    def welcome():
        return "now you are in the welcome() function"

    if name == "enthusiasm":
        return greet
    else:
        return welcome
    

a = hi()
print(a)
print(a())

output=

<function hi.<locals>.greet at 0x1043d3700>

now you are in the greet() function

再次看看这个代码,在if/else语句中我们返回greet和welcome,而不是greet()和welcome()。为什么呢?这是因为当我们把一对小括号放在后面,这个函数就会执行;然而在后面不放括号,它可以被到处传递,并且可以赋值给别的变量而不去执行它。

我们写下a = hi() hi()会被执行,而由于name参数默认是enthusiasm,所以函数greet被返回。如果我们语句改为a = hi(name = "happy"),那么welcome函数将被返回。我们还可以打印出hi()()即a(),输出"now you are in the greet() function"

4.将函数作为参数传给另一个函数

def hi():
    return "hi goodbai"

def doSomethingBeforeHi(func):
    print("I am doing some boring work before executing hi()")
    print(func())


doSomethingBeforeHi(hi)

4.1注意🔗:

装饰器让你在一个函数前后去执行代。

5.第一个装饰器

在上一个例子里,我们已经创建了一个装饰器,现在我们修改下上一个装饰器,并编写一个具备更多功能的程序:

def a_new_decorator(a_func):

    def wrapTheFunction():
        print("I am doing some boring work before executing a_func()")
        
        a_func()

        print("I am doing some boring work after executing a_func()")

    return wrapTheFunction

def a_function_requiring_decoration():
    print("I am the function which needs some decoration to remove my foul sell")

a_function_requiring_decoration()

 now, a_function_requiring_decoration is wrapped by wrapTheFunction()

def a_new_decorator(a_func):

    def wrapTheFunction():
        print("I am doing some boring work before executing a_func()")
        
        a_func()

        print("I am doing some boring work after executing a_func()")

    return wrapTheFunction

def a_function_requiring_decoration():
    print("I am the function which needs some decoration to remove my foul sell")

a_function_requiring_decoration()

a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

a_function_requiring_decoration()

我们刚刚应用到了之前学习到的原理。这正是python中装饰器做的事情!它们封装一个函数,并且用到这样或者那样的方式来修改他的行为。我们在代码里并没有使用@符号?那是一个简短的方式来生成一个被装饰的函数,我们使用@来运行之前的代码:

from re import A

def a_new_decorator(a_func):

    def wrapTheFunction():
        print("I am doing some boring work before executing a_func()")
        
        a_func()

        print("I am doing some boring work after executing a_func()")

    return wrapTheFunction

def a_function_requiring_decoration():
    print("I am the function which needs some decoration to remove my foul sell")

a_function_requiring_decoration()

a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

a_function_requiring_decoration()

@a_new_decorator
def a_function_requiring_decoration():
    print("I am the function which needs some decoration to remove my foul smell")

a_function_requiring_decoration()
    

 我们目前运行如下代码会存在一个问题:

print(a_function_requiring_decoration.__name__)
# Output: wrapTheFunction

这并不是我们想要的输出结果,想要的输出结果应该是"a_function_requiring_decoration",这里的函数被wrapTheFunction替代了,它重写了我们函数的名字和注释文档(docstring)。幸运的是,Python提供给我们一个简单的函数来解决这个问题,那就是functools.wraps。我们修改上一个例子,来使用functools.wraps:

from functools import wraps

def a_new_decorator(a_func):
    @wraps(a_func)
    def wrapTheFunction():
        print("I am doing some boring work before executing a_func()")
        a_func()
        print("I am doing some boring work after executing a_func()")
    return wrapTheFunction

@a_new_decorator
def a_function_requiring_decoration():
    print("I am the function which needs some decoration to remove my foul smell")

print(a_function_requiring_decoration.__name__)

5.1装饰器的一些常用场景

5.1.1蓝本规范

from functools import wraps
def decorator_name(f):
    @wraps(f)
    def decorated(*args, **kargs):
        if not can_run:
            return "Function will not run"
        return f(*args, **kargs)
    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 wil not running

5.1.2注意🔗

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

5.2使用场景

让一些事情管理起来更简单。

5.2.1授权(Authorization)

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

from functools import wraps

from requests import request

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

5.3日志(Logging)

日志是装饰器运用的另一个亮点。

from functools import wraps

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

@logit
def addition_func(x):
    return x + x

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

6.带参数的装饰器

思考一个问题:@ wraps不也是个装饰器吗?它接受一个参数,就像任何普通的函数能做的那样。为什么我们不也那样做呢?因为,当我们使用@my_decorator语法时,你是在应用一个以单个函数作为参数的一个包裹函数。Python里每个东西都是一个对象,而且这包括函数!我们编写一个能返回一个包裹函数的函数。

6.1在函数中嵌入装饰器

我们回到日志的例子,并创建一个包裹函数,能让我们指定一个用于输出的日志文件。

from functools import wraps
 
def logit(logfile='out.log'):
    def logging_decorator(func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + " was called"
            print(log_string)
            # 打开logfile,并写入内容
            with open(logfile, 'a') as opened_file:
                # 现在将日志打到指定的logfile
                opened_file.write(log_string + '\n')
            return func(*args, **kwargs)
        return wrapped_function
    return logging_decorator
 
@logit()
def myfunc1():
    pass
 
myfunc1()
# Output: myfunc1 was called
# 现在一个叫做 out.log 的文件出现了,里面的内容就是上面的字符串
 
@logit(logfile='func2.log')
def myfunc2():
    pass
 
myfunc2()
# Output: myfunc2 was called
# 现在一个叫做 func2.log 的文件出现了,里面的内容就是上面的字符串

6.2装饰器类

现在我们有了能用于正式环境的logit装饰器,但当我们的应用的某些部分还比较脆弱时,异常需要关注!

比方说有时只想打日志到一个文件。而有时想某个问题发送到一个email,同时也保留日志,留个记录。这是一个使用继承的场景,但目前为止我们只看到过用来构建装饰器的函数。幸运的是,类也可以用来构建装饰器。那我们现在以一个类而不是一个函数的方式,来重新构建logit。

from functools import wraps

class logit(object):
    def __init__(self, logfile='out.log'):
        self.logfile = logfile

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + "was called"
            print(log_string)
            #打开logfile并写入
            with open(self.logfile, 'a')as opened_file:
                #现在将日志打到指定的文件
                opened_file.write(log_string + '\n')
            #现在,发送一个通知
            self.notify()
            return func(*args, **kwargs)
        return wrapped_function
    
    def notify(self):
        # logit只打日志,不做别的
        pass

 这个实现有一个附加优势,在于比嵌套函数的方式更加整洁,而且包裹一个函数还使用跟以前一样的语法:

@logit()
def myfunc1():
    pass

6.2.1添加email功能

我们现在给logit创建子类,来添加email的功能,一个logit的实现版本,可以在函数调用时发送email给管理员,还可以发送一封email到self.email.

from functools import wraps

class logit(object):
    def __init__(self, logfile='out.log'):
        self.logfile = logfile

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            log_string = func.__name__ + "was called"
            print(log_string)
            #打开logfile并写入
            with open(self.logfile, 'a')as opened_file:
                #现在将日志打到指定的文件
                opened_file.write(log_string + '\n')
            #现在,发送一个通知
            self.notify()
            return func(*args, **kwargs)
        return wrapped_function
    
    def notify(self):
        # logit只打日志,不做别的
        pass

#一个logit的实现版本,可以在函数调用时发送email给管理员
class email_logit(logit):
    def __init__(self, email='admin@my project.com', *args, **kwargs):
        self.email = email
        super(email_logit, self).__init__(*args, **kwargs)


@email_logit 将会和 @logit 产生同样的效果,但是在打日志的基础上,还会多发送一封邮件给管理员。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值