Python Decorators(二):Decorator参数

2023-10-26

Python Decorators II: Decorator Arguments

October 19, 2008

 

(本文是Python 3 Patterns & IdiomsPython3之模式和用法)一书的章节节选第二部分,点击这里阅读第一部分)

 

回顾:不含参数的decorators

在前文中,我介绍了如何使用不含参数的decorators,并使用类来实现。因为我发现这样做更容易接受。

如果创建了一个无参decorator,被decorated的函数被传至构造器,每次调用decorated函数时就会调用__call__()方法:

 

class decoratorWithoutArguments(object):

 

    def __init__(self, f):

        """

        If there are no decorator arguments, the function

        to be decorated is passed to the constructor.

        """

        print "Inside __init__()"

        self.f = f

 

    def __call__(self, *args):

        """

        The __call__ method is not called until the

        decorated function is called.

        """

        print "Inside __call__()"

        self.f(*args)

        print "After self.f(*args)"

 

@decoratorWithoutArguments

def sayHello(a1, a2, a3, a4):

    print 'sayHello arguments:', a1, a2, a3, a4

 

print "After decoration"

 

print "Preparing to call sayHello()"

sayHello("say", "hello", "argument", "list")

print "After first sayHello() call"

sayHello("a", "different", "set of", "arguments")

print "After second sayHello() call"

 

decorated函数的所有参数会被传至__call__()。输出结果是:

 

Inside __init__()

After decoration

Preparing to call sayHello()

Inside __call__()

sayHello arguments: say hello argument list

After self.f(*args)

After first sayHello() call

Inside __call__()

sayHello arguments: a different set of arguments

After self.f(*args)

After second sayHello() call

 

注意,__init__()是唯一一个被调用执行decoration的方法,每次调用decoratedsayHello()时就会调用__call__()

 

含有参数的decorators

现在让我们来修改上面的代码,看看向decorator加入参数后结果是什么。

 

class decoratorWithArguments(object):

 

    def __init__(self, arg1, arg2, arg3):

        """

        If there are decorator arguments, the function

        to be decorated is not passed to the constructor!

        """

        print "Inside __init__()"

        self.arg1 = arg1

        self.arg2 = arg2

        self.arg3 = arg3

 

    def __call__(self, f):

        """

        If there are decorator arguments, __call__() is only called

        once, as part of the decoration process! You can only give

        it a single argument, which is the function object.

        """

        print "Inside __call__()"

        def wrapped_f(*args):

            print "Inside wrapped_f()"

            print "Decorator arguments:", self.arg1, self.arg2, self.arg3

            f(*args)

            print "After f(*args)"

        return wrapped_f

 

@decoratorWithArguments("hello", "world", 42)

def sayHello(a1, a2, a3, a4):

    print 'sayHello arguments:', a1, a2, a3, a4

 

print "After decoration"

 

print "Preparing to call sayHello()"

sayHello("say", "hello", "argument", "list")

print "after first sayHello() call"

sayHello("a", "different", "set of", "arguments")

print "after second sayHello() call"

 

从输出结果可以看到,加入参数使程序执行发生了很大变化。

 

Inside __init__()

Inside __call__()

After decoration

Preparing to call sayHello()

Inside wrapped_f()

Decorator arguments: hello world 42

sayHello arguments: say hello argument list

After f(*args)

after first sayHello() call

Inside wrapped_f()

Decorator arguments: hello world 42

sayHello arguments: a different set of arguments

After f(*args)

after second sayHello() call

 

现在decoration方法调用构造器,然后就马上调用__call__(),后者只能包含一个参数(函数对象)且返回替代原有函数的decorated函数对象。注意当前decoration期间__call__()仅被调用一次,此后从__call__()返回的decorated函数就可以在实际调用中使用了。

虽然这种机制有一定合理性构造器在这里可获取decorator参数,但__call__()对象不能再作为decorated函数使用了。因此你必须使用__call__()执行decoration—可能第一次遇到这种与无参情况截然不同的方式你会比较吃惊,何况还必须编写和无参decorator完成不同的代码。

 

decorator参数的decorator函数

最后,让我们看一个更复杂一点的decorator函数实现,它需要你处理所有细节:

 

def decoratorFunctionWithArguments(arg1, arg2, arg3):

    def wrap(f):

        print "Inside wrap()"

        def wrapped_f(*args):

            print "Inside wrapped_f()"

            print "Decorator arguments:", arg1, arg2, arg3

            f(*args)

            print "After f(*args)"

        return wrapped_f

    return wrap

 

@decoratorFunctionWithArguments("hello", "world", 42)

def sayHello(a1, a2, a3, a4):

    print 'sayHello arguments:', a1, a2, a3, a4

 

print "After decoration"

 

print "Preparing to call sayHello()"

sayHello("say", "hello", "argument", "list")

print "after first sayHello() call"

sayHello("a", "different", "set of", "arguments")

print "after second sayHello() call"

 

输出结果为:

 

Inside wrap()

After decoration

Preparing to call sayHello()

Inside wrapped_f()

Decorator arguments: hello world 42

sayHello arguments: say hello argument list

After f(*args)

after first sayHello() call

Inside wrapped_f()

Decorator arguments: hello world 42

sayHello arguments: a different set of arguments

After f(*args)

after second sayHello() call

 

decorator函数的返回值必须是一个封装待decorated函数的函数。也就是说,Python会保存返回函数然后在decoration期间调用,并传递待decorated函数。这也是为何有三层函数的原因:里面那个函数才是被替换的。

由于闭包,wrapped_f()有权访问decorator参数arg1, arg2 arg3,而无需像在class版本中那样显式存储它们。然而,我也是在这里发现了“显胜于隐(explicit is better than implicit)”。即使该函数版本看起来要更加简洁紧凑,但我发现还是类版本容易理解,当然也就容易修改和维护。

 

下一节内容

在下一节中我会给出decorators的一些实例基于Python开发的build system—然后在最后一节讨论类decorators

 

(原文链接网址:http://www.artima.com/weblogs/viewpost.jsp?thread=240845

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python Decorators(二):Decorator参数 的相关文章

  • Python lambda 函数没有在 for 循环中正确调用[重复]

    这个问题在这里已经有答案了 我正在尝试使用 Python 中的 Tkinter 制作一个计算器 我使用 for 循环来绘制按钮 并且尝试使用 lambda 函数 以便仅在按下按钮时调用按钮的操作 而不是在程序启动时立即调用 然而 当我尝试这
  • 数据操作 startdate enddate python pandas

    我有一个促销描述数据集 其中包含有关正在运行的各种促销活动及其开始日期 结束日期的信息 promo item start date end date Buy1 get 1 A 2015 01 08 2015 01 12 Buy1 get 1
  • 以类似字典的方式将新项目添加到某些结构化数组中

    我想扩展 numpy 中的结构化数组对象 以便我可以轻松添加新元素 例如 对于一个简单的结构化数组 gt gt gt import numpy as np gt gt gt x np ndarray 2 dtype names A B fo
  • 隐藏控制台并执行 python 脚本

    我正在尝试使用 pyinstaller 在 Windows 10 上使用 pyqt5 模块编译在 python 3 中构建的 python 脚本 该脚本在运行时隐藏窗口 为了编译我的脚本 我执行了以下命令 pyinstaller onefi
  • 如何在cvxpy中编写多个约束?

    我想在 cvxpy 下的优化问题中添加许多约束 在 matlab 中 我可以通过添加一行 subject to 然后使用 for 循环来生成约束 我怎样才能在 cvxpy 中做同样的工作 因为 cvxpy 中没有 服从 概念 有什么建议吗
  • 从 SQL Server 中调用 Python 文件

    我的文件名中有 Python 脚本 C Python HL py 在此 Python 脚本中 有预测模型以及对 SQL 数据库中某些表的更新 我想将此文件称为 SQL 作业 我怎样才能做到这一点 这个问题不一样 如何在 SQL Server
  • Django url 模式 - 带正斜杠的参数

    如何为两个参数创建 url 模式 其中第一个参数包含正斜杠作为其内容的一部分 da ta1 data2 最初我有以下模式 r view P
  • VSCode Jupyter Notebook - 恢复缓存版本

    我正在使用在 Ubuntu 19 10 上运行的 VSCode v 1 48 0 创建一个 Jupyter Notebook VSCode 崩溃了 不幸的是我没有保存笔记本 当我重新启动时它是空的 我已经能够在 config Code Us
  • 可重用的 Tensorflow 卷积网络

    我想重用来自Tensorflow 专业人士的 MNIST CNN 示例 http www tensorflow org tutorials mnist pros index md 我的图像尺寸为 388px X 191px 只有 2 个输出
  • 遍历 globals() 字典

    我 尝试 使用globals 在我的程序中迭代所有全局变量 我就是这样做的 for k v in globals iteritems function k v 当然 这样做时 我只是创建了另外 2 个全局变量 k and v 所以我得到这个
  • 使用 Beautifulsoup 解析时保持 XML 文件的缩进

    我正在使用 BS4 解析 XML 文件并尝试将其写回新的 XML 文件 输入文件
  • pip:证书失败,但curl 有效

    我们在客户端安装了根证书 https 连接适用于curl 但如果我们尝试使用pip 它失败 Could not fetch URL https installserver 40443 pypi simple pep8 There was a
  • 带回溯的 Dijkstra 算法?

    In a 相关主题 https stackoverflow com questions 28333756 finding most efficient path between two nodes in an interval graph
  • 如何点击 Google Trends 中的“加载更多”按钮并通过 Selenium 和 Python 打印所有标题

    这次我想单击一个按钮来加载更多实时搜索 这是网站的链接 该按钮位于页面末尾 代码如下 div class feed load more button Load more div 由于涉及到一些 AngularJS 我不知道该怎么做 有什么提
  • 在 python 中计时时,我应该如何考虑 subprocess.Popen() 开销?

    编码社区的成员比我更聪明 我有一个 python 问题要问你们 我正在尝试优化一个 python 脚本 该脚本 除其他外 返回子进程执行和终止的挂钟时间 我想我已经接近这样的事情了 startTime time time process s
  • 对 Python 的 id() 感到困惑[重复]

    这个问题在这里已经有答案了 我可以理解以下定义 每个对象都有一个身份 类型和值 对象的身份 一旦创建就永远不会改变 你可能会认为它是 对象在内存中的地址 这is操作员比较身份 两个物体 这id 函数返回一个代表其值的整数 身份 我假设上面的
  • 使用 PIL 合并图像时模式不匹配

    我正在传递 jpg 文件的名称 def split image into bands filename img Image open filename data img getdata red d 0 0 0 for d in data L
  • 如何克服 numpy.unique 的 MemoryError

    我正在使用 Numpy 版本 1 11 1 并且必须处理一个二维数组 my arr shape 25000 25000 所有值都是整数 我需要一个唯一的数组值列表 使用时lst np unique my arr 我正进入 状态 Traceb
  • Windows 10 上的 Tensorflow 安装问题

    我正在尝试在 Win 10 计算机上安装 Tensorflow 我成功安装了Python 3 7 然后尝试按照tensorflow org上的安装说明进行操作 执行时 pip install tensorflow 我收到以下错误消息 错误
  • 如何访问模板缓存? - 姜戈

    I am 缓存 HTML在几个模板内 例如 cache 900 stats stats endcache 我可以使用以下方式访问缓存吗低级图书馆 例如 html cache get stats 我确实需要对模板缓存进行一些细粒度的控制 有任

随机推荐

  • mysql驱动协议之loadbalance和replication

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 背景 偶然下和朋友聊到了mysql多节点集群架场景 对应我们各系代码如何去使用它 牵扯到mysql的驱动包中已经支持的策略 发现这块的概念有些模糊 索性就梳理了一番留着后用
  • 如何防止mq的消息丢失

    分为两种情况 1 主要在消费方 当消息从mq传到消费者时 消费者可能消费消息失败 这时mq中的消息已经自动删除了 导致消息的丢失 需要使用ack机制来保证消息不会丢失 当消费者从mq中拿到消息时 这个消息在mq中不删除 而是消费者对消息进行
  • Qt实现2D绘图

    一 介绍 Qt中提供了强大的2D绘图系统 可以使用相同的API在屏幕和绘图设备上进行绘制 它主要基于QPainter QPaintDevice和QPaintEngine这三个类 其中QPainter用来执行绘图操作 QPaintDevice
  • 科技抗老新突破,香港美容仪品牌内地重磅上市

    近年来 新消费时代 颜值经济 的火热促使美容行业市场规模增长迅速 越来越多的人愿意为 美 买单 对美的需求也随之增长 美容行业已经成为成长最快的新锐产业 随着经济和科技的发展 快捷 也成为了当今社会的时代特征 美容行业也从传统的美容院向家用
  • 竞赛选题 opencv 图像识别 指纹识别 - python

    0 前言 优质竞赛项目系列 今天要分享的是 基于机器视觉的指纹识别系统 学长这里给一个题目综合评分 每项满分5分 难度系数 3分 工作量 3分 创新点 4分 该项目较为新颖 适合作为竞赛课题方向 学长非常推荐 更多资料 项目分享 https
  • 前端安装vue-cli报错:npm ERR! notarget No matching version found for vue@cli.

    安装vue cli的版本3以上的内容 命令中 在最前 不在中间 对的命令是npm install g vue cli C Users 15232 gt npm install g vue cli npm ERR code ETARGET n
  • Linux 安装Mysql 详细教程(图文教程)

    首先通过xshell或者 putty 远程进入Linux 命令行操作界面 Xshell 的安装 1 去XShell Download下载需要的版本 XShell免费版 解决官网打不开的问题 百度网盘 https pan baidu com
  • 用6个实例,8段代码,详解Python中的for循环

    目录 前言 01 使用tryexcept的for循环 02 指数运算 03 嵌套的循环 04 在for循环中使用split 函数 1 使用split 函数做单词比较 2 使用split 函数打印指定格式的文本 3 使用split 函数打印固
  • windows 7Z命令行与安装

    7z 全称7 Zip 是一款开源软件 是目前公认的压缩比例最大的压缩解压软件 7z exe在CMD窗口的使用说明如下 7 Zip A 4 57 Copyright c 1999 2007 Igor Pavlov 2007 12 06 Usa
  • colmap代码解读

    clomap是作者在ECCV2016年发表的基于两个概率的深度值和法线估计的论文 开源 下面就开源代码Patch match cuda cu文件做简单的介绍 产生随机法向量和随机深度值 扰动法向量 产生随机三个方位角度 和扰动深度值 根据像
  • 多线程案例(2) - 阻塞队列

    目录 一 阻塞队列 1 1 什么是阻塞队列 1 2 生产者消费者模型 1 3 标准库中的阻塞队列 1 4 阻塞队列的实现 一 阻塞队列 1 1 什么是阻塞队列 阻塞队列 BlockingQueue 是一种特殊的队列 遵循 先进先出 的原则
  • Deep-Learning-YOLOV4实践:ScaledYOLOv4模型训练自己的数据集调试问题总结

    error error1 CUDA out of memory error2 TypeError can t convert cuda error Deep Learning YOLOV4实践 ScaledYOLOv4 数据集制作 Deep
  • 知识库-kafka shell脚本用法

    脚本名称 用途描述 connect distributed sh 连接kafka集群模式 connect standalone sh 连接kafka单机模式 kafka acls sh todo kafka broker api versi
  • 一篇搞定dockerfile定制镜像过程

    一 定制镜像的两种方法 1 docker commit 通过已有容器创建镜像 提交容器快照作为镜像 不推荐 2 docker build 就是本文着重讲的dockerfile创建镜像方式 推荐 docker commit无法还原镜像制作过程
  • 【Linux学习】epoll详解

    什么是epoll epoll是什么 按照man手册的说法 是为处理大批量句柄而作了改进的poll 当然 这不是2 6内核才有的 它是在2 5 44内核中被引进的 epoll 4 is a new API introduced in Linu
  • centos7运行vue项目问题汇总

    一 node踩坑之This is probably not a problem with npm There is likely additional logging output above 错误 解决步骤 1 可能由于种种版本更新的原因
  • windbg 常用命令详解

    一 1 address eax 查看对应内存页的属性 2 vertarget 显示当前进程的大致信息 3 peb 显示process Environment Block 4 lmvm 可以查看任意一个dll的详细信息 例如 我们查看cyus
  • java中List按照指定字段排序工具类

    文章标题 java中List按照指定字段排序工具类 文章地址 http blog csdn net 5iasp article details 17717179 包括如下几个类 1 实体类 package com newyear wish
  • 【C语言】螺旋数组

    螺旋数组的打印 程序C语言代码 更改宏定义的数值即可实现螺旋数组行列的变化 include stdio h define ROW 5 宏定义行 define COL 5 宏定义列 void main int arr ROW COL 0 in
  • Python Decorators(二):Decorator参数

    Python Decorators II Decorator Arguments October 19 2008 本文是Python 3 Patterns Idioms Python3之模式和用法 一书的章节节选第二部分 点击这里阅读第一部