你的第一个装饰器是怎么写的

本文字数:1016 字 || 

阅读时间3 

分钟 "

其实我们已经创建了一个装饰器! 一切皆对象,那是我的对象

现在我们修改下上一个装饰器,并编写一个稍微更有用点的程序:

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 smell"


a_function_requiring_decoration()
#outputs: "I am the function which needs some decoration to remove my foul smell"


a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)
#now a_function_requiring_decoration is wrapped by wrapTheFunction()


a_function_requiring_decoration()
#outputs:I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()

这正是 Python 中装饰器做的事情!

它 们封装一个函数,并且这样或者那样的方式来修改它的行。现在你也许疑惑,我们在代码里并没有使用@符号?那只是一个简短的方式来生成一个被装饰的函数。

这里是我们如何使用@来运行之前的代码:

@a_new_decorator
def a_function_requiring_decoration():
    """Hey you! Decorate me!"""
    print("I am the function which needs some decoration to "
                  "remove my foul smell")
 
 a_function_requiring_decoration()
#outputs: I am doing some boring work before executing a_func()
# I am the function which needs some decoration to remove my foul smell
# I am doing some boring work after executing a_func()
#the @a_new_decorator is just a short way of saying:
a_function_requiring_decoration = a_new_decorator(a_function_requiri

希望你现在对 Python 装饰器的工作原理有个基本的理解。

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

print(a_function_requiring_decoration.__name__)
# Output: wrapTheFunction

这并不是我们想要的!

Ouput输出应该是“a_function_requiring_decoration”。这里的函数被 warpTheFunction替代了。它重写了我们函数的名字和注释文档(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():
                """Hey yo! Decorate me!"""
    print("I am the function which needs some decoration to "
          "remove my foul smell")
print(a_function_requiring_decoration.__name__)
# Output: a_function_requiring_decoration

现在好多了。

我们接下来学习装饰器的一些常用场景。 

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接受一个函数来进行装饰,并加个了复制函数名称、注释文档、参数列表 等等的功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性。

程序员的资源聚集地,你想要的各类编程资源,都可以来这里找

推荐阅读

一切皆对象,那是我的对象

简洁疫情查询服务上线

Python 进阶之术  Map Filter Reduce

程序员资源社区

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值