基于 Python 类的装饰器,带有可以装饰方法或函数的参数

本文介绍了基于 Python 类的装饰器,带有可以装饰方法或函数的参数的处理方法,对大家解决问题具有一定的参考价值

问题描述

我见过很多 Python 装饰器的例子:

I've seen many examples of Python decorators that are:

  • 函数样式装饰器(包装函数)
  • 类样式装饰器(实现 __init____get____call__)
  • 不带参数的装饰器
  • 接受参数的装饰器
  • 方法友好"的装饰器(即可以装饰类中的方法)
  • 功能友好"的装饰器(可以装饰普通功能
  • 可以装饰方法和函数的装饰器

但我从未见过一个可以完成上述所有操作的示例,而且我无法从特定问题的各种答案中综合起来(例如 这个这个,或这个(这是我在SO上见过的最好的答案之一)),如何结合以上所有内容.

But I've never seen a single example which can do all of the above, and I'm having trouble synthesizing from various answers to specific questions (such as this one, this one, or this one (which has one of the best answers I've ever seen on SO)), how to combine all of the above.

我想要的是一个基于类的装饰器,它可以装饰方法或函数至少需要一个附加参数.即,以便以下工作:

What I want is a class-based decorator which can decorate either a method or a function, and that takes at least one additional parameter. Ie so that the following would work:

class MyDecorator(object):
    def __init__(self, fn, argument):
        self.fn = fn
        self.arg = argument

    def __get__(self, ....):
        # voodoo magic for handling distinction between method and function here

    def __call__(self, *args, *kwargs):
        print "In my decorator before call, with arg %s" % self.arg
        self.fn(*args, **kwargs)
        print "In my decorator after call, with arg %s" % self.arg


class Foo(object):
    @MyDecorator("foo baby!")
    def bar(self):
        print "in bar!"


@MyDecorator("some other func!")
def some_other_function():
    print "in some other function!"

some_other_function()
Foo().bar()

我希望看到:

In my decorator before call, with arg some other func!
in some other function!
In my decorator after call, with arg some other func!
In my decorator before call, with arg foo baby!
in bar!
In my decorator after call, with arg foo baby!

如果重要的话,我使用的是 Python 2.7.

if it matters, I'm using Python 2.7.

推荐答案

你不需要乱用描述符.在 __call__() 方法中创建一个包装函数并返回它就足够了.标准 Python 函数始终可以充当方法或函数,具体取决于上下文:

You don't need to mess around with descriptors. It's enough to create a wrapper function inside the __call__() method and return it. Standard Python functions can always act as either a method or a function, depending on context:

class MyDecorator(object):
    def __init__(self, argument):
        self.arg = argument

    def __call__(self, fn):
        @functools.wraps(fn)
        def decorated(*args, **kwargs):
            print "In my decorator before call, with arg %s" % self.arg
            result = fn(*args, **kwargs)
            print "In my decorator after call, with arg %s" % self.arg
            return result
        return decorated

关于当这个装饰器这样使用时发生了什么的一点解释:

A bit of explanation about what's going on when this decorator is used like this:

@MyDecorator("some other func!")
def some_other_function():
    print "in some other function!"

第一行创建了一个MyDecorator 的实例,并将一些其他函数!" 作为参数传递给__init__().我们称这个实例为 my_decorator.接下来,未装饰的函数对象——我们称之为bare_func——被创建并传递给装饰器实例,因此my_decorator(bare_func)被执行.这将调用 MyDecorator.__call__(),它将创建并返回一个包装函数.最后,这个包装函数被赋予名称some_other_function.

The first line creates an instance of MyDecorator and passes "some other func!" as an argument to __init__(). Let's call this instance my_decorator. Next, the undecorated function object -- let's call it bare_func -- is created and passed to the decorator instance, so my_decorator(bare_func) is executed. This will invoke MyDecorator.__call__(), which will create and return a wrapper function. Finally this wrapper function is assigned to the name some_other_function.

这篇关于基于 Python 类的装饰器,带有可以装饰方法或函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

WordPress使用python会话上载文件

我需要上传图像到wordpress编程,理想情况下没有安装额外的插件。不过,我对涉及插件的最佳实践持开放态度。到目前为止,我已经能够使用会话登录和移动站点,但是当我尝试将文件上载到媒体时-新建.php或异步-上传.php我收到以下错误消息The file is a test text file with a single line (also the upload limit on the site is 1GB) so it\"s not the common file size limit. This ...

日期:2021-08-21 05:00:01 浏览:792

压缩序列化的Python数据最节省空间的方法是什么?

本文介绍了压缩序列化的Python数据最节省空间的方法是什么?的处理方法,对大家解决问题具有一定的参考价值 问题描述 发件人the Python documentation:默认情况下,Pickle数据格式使用相对紧凑的二进制表示。如果您需要最佳大小特性,您可以高效地压缩酸洗数据。我将在一个运行了几个小时的过程结束时序列化...

日期:2022-06-23 08:50:34 浏览:758

在Python中,有没有一种方法可以将一个单词分割成等分?

本文介绍了在Python中,有没有一种方法可以将一个单词分割成等分?的处理方法,对大家解决问题具有一定的参考价值 问题描述 几周前我问了这个问题,得到了答案this is the original post但我需要将输出分成相等的部分,无论字符串的长度如何,所以在我发布的第一个帖子中,我得到了这个答案,它很好地工作了,这要...

日期:2022-06-24 06:52:06 浏览:483

如何在 Google AppEngine Python37 中获取凭据

本文介绍了如何在 Google AppEngine Python37 中获取凭据的处理方法,对大家解决问题具有一定的参考价值 问题描述 我在 AppEngine Python3.7 标准中启动了新应用.I started new app in AppEngine Python3.7 stadard.我正在尝试使用以下代码段...

日期:2022-06-24 09:00:27 浏览:550

为什么 python 字符串和元组是不可变的?

本文介绍了为什么 python 字符串和元组是不可变的?的处理方法,对大家解决问题具有一定的参考价值 问题描述 我不确定为什么字符串和元组是不可变的;使它们不可变的优点和缺点是什么?I am not sure why strings and tuples were made to be immutable; what ar...

日期:2022-06-24 09:00:30 浏览:891

使用 Python 解析 Gmail 并将所有早于日期的内容标记为“已读"

本文介绍了使用 Python 解析 Gmail 并将所有早于日期的内容标记为“已读"的处理方法,对大家解决问题具有一定的参考价值 问题描述 长话短说,我创建了一个新的 gmail 帐户,并将其他几个帐户关联到该帐户(每个帐户都有 1000 条消息),我正在导入这些帐户.所有导入的邮件都以未读的形式到达,但我需要它们显示为已...

日期:2022-06-24 10:00:29 浏览:810

了解python线程错误

本文介绍了了解python线程错误的处理方法,对大家解决问题具有一定的参考价值 问题描述 阅读http://bugs.python.org/msg160297,我可以看到Stephen White编写的一个简单脚本,它演示了该异常是如何导致python线程出错的Exception AttributeError: Attri...

日期:2022-06-24 21:00:28 浏览:951

从python调用url时获取“错误"的页面源

本文介绍了从python调用url时获取“错误"的页面源的处理方法,对大家解决问题具有一定的参考价值 问题描述 尝试从网站检索页面源时,得到的文本与通过 Web 浏览器查看相同页面源时完全不同(且更短).Trying to retrieve the page source from a website, I get a c...

日期:2022-06-25 01:00:31 浏览:605

基于 Python 类的装饰器,带有可以装饰方法或函数的参数

本文介绍了基于 Python 类的装饰器,带有可以装饰方法或函数的参数的处理方法,对大家解决问题具有一定的参考价值 问题描述 我见过很多 Python 装饰器的例子:I've seen many examples of Python decorators that are:函数样式装饰器(包装函数)类样式装饰器(实现 __...

日期:2022-06-25 04:00:31 浏览:937

用python解析outlook .msg文件

本文介绍了用python解析outlook .msg文件的处理方法,对大家解决问题具有一定的参考价值 问题描述 环顾四周,没有找到满意的答案.有谁知道如何使用 Python 解析 Outlook 中的 .msg 文件?Looked around and couldn't find a satisfactory answer...

日期:2022-06-25 06:00:30 浏览:643