首页 > 编程语言 >Python - Decorating classes

Python - Decorating classes

时间:2024-07-31 15:28:34浏览次数:7  
标签:methods Python argument will classes Decorating class decorator first

When we talk about decorating classes, we can either decorate individual methods or we can create a decorator to decorate the whole class.

class MyClass:
    def __init__(self, a):
        self.a = a

    @timer
    def method1(self, x, y):
        print('method1 executing')

    @timer
    def method2(self, x, y):
        print('method2 executing')

    @trace
    def method3(self, x, y):
        print('method3 executing')

Decorating methods is straightforward, it is like decorating functions only. For example, the decorators trace and timer that we have created in our lectures, can be applied to methods of a class to trace or time the method calls. While applying the decorators to methods of a class, we need to keep in mind that a method always receives the current instance as the first argument. The decorators trace and timer that we had created, will work properly with methods also because they are not doing anything special with the first argument but suppose you have a decorator that sanitizes the first argument of a function then you cannot simply apply that decorator to a method, because for methods, the current instance is the first argument, and the first argument that you send when you call the method is actually the second argument.

 

Now, let us see how to create a decorator function to decorate the class as a whole. This type of decorator function will take a class as the argument. It will either modify that class and return it, or it will create a new class and return it. Modifying the class in-place and returning the modified class is more convenient than creating a new class. So, we will see some examples where we will create a decorator that takes a class and returns the modified class.

The syntax for applying the decorator to a class will be the same. You can either use the automatic way or decorate the class manually.

@decorator
class MyClass:
    pass


class MyClass:
    pass
MyClass = decorator(MyClass)

 

标签:methods,Python,argument,will,classes,Decorating,class,decorator,first
From: https://www.cnblogs.com/zhangzhihui/p/18334748

相关文章

  • 如何在 python 中使用矩阵分解从 3 个不同的矩阵(2 个交互矩阵和 1 个相似矩阵)创建一个
    我有3个不同大小的矩阵A、B、C,它们有一些共同的元素。A(相似性)与B(交互)有一些共同元素,但与C(第二交互矩阵)没有共同元素。B有一些与C相同的元素。我想创建一个基于B和C的矩阵M,但提供基于A、B、C的输出,就像推荐系统类型一样,其中根据评分、观看次数和偏好推荐电影。例......
  • Python - Decorators
    Adecoratorisacallablethattakesacallableasinputandreturnsacallable.Thisisthegeneraldefinitionofadecorator.Thecallableinthisdefinitioncanbeafunctionoraclass.Inourinitialdiscussion,wewilltalkaboutdecoratorfunctions......
  • Python捕获一组中的1000个项目进行处理
    我有一个包含数千条记录的大表(可能有3,000到75,000条记录),我将所有数字ID放入排序列表中。我想一次有序地处理一组1000个ID。我如何优雅地获取前1000个和“标签”,设置为“223344到337788”(字典在这里有意义吗,或者只是列表捕获中的第一个/最后一个项目......以跟......
  • 在 Python 中创建和/或检查编号变量的优雅方法
    我是一个试图学习Python的老家伙,所以我最后的编码经验是使用BASIC-不,不是VisualBasic。我理解一些与Python相关的概念,但我处于初级编码阶段,所以我使用“强力”逻辑编写了这个项目-基本上,将字符串分解为单个字母,然后用经典的“”测试每个字母猜单词类型的游戏。......
  • 使用法兰克福 API 和 Python 虚拟环境时出现 404 错误
    我正在VisualStudioCode中用Python制作货币转换器脚本,并且使用法兰克福API,它在几天前一直工作正常,直到现在每次我尝试获取API时都会收到404错误:|||输出:>response=requests.get(f"https://api.frankfurter.app/latest?amount={amount}&from={CF}&to={CT}")>......
  • 我的目标是检测车道并控制车辆保持在车道中央。使用Python
    我目前正在做一个项目,我是一个初学者。并且我需要找到一种方法来使用检测到的车道来控制我的项目车辆保持在两条线之间的中心。img1|||img2我有疑问的话题如下如何判断我的机器人车是否在车道中央?我们应该用什么方法来控制机器人的转向......
  • 【学习笔记】Matlab和python双语言的学习(主成分分析法)
    文章目录前言一、主成分分析法1.主成分分析法简介2.主成分分析法原理3.主成分分析法思想4.PCA的计算步骤二、代码实现----Matlab三、代码实现----python总结前言通过模型算法,熟练对Matlab和python的应用。学习视频链接:https://www.bilibili.com/video/BV1EK41187......
  • 如何使用python输入提示具有相同参数类型但不同返回类型的函数?
    我有一个函数,它的返回类型是tuple[bool,set[int]|str]如果第0项是True,则第1项是结果set[int],否则第1项是一个str,显示失败的原因。是这样的defcallee(para_a:int)->tuple[bool,set[int]|str]:result=set([1,2,3])if......
  • 彻底卸载Python
        前言通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装。所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Python 软件卸载方法1:首先,在安装python时,下载了一个可执行文件,也就是Python的安装包,我们双击它,点击uninstal......
  • 如何使用 Azure Devops API (Python) 提取特定提交的文件内容?
    这就是我想要做的:每次对我的存储库中的特定分支进行提交时,我想提取该提交中更改的所有YAML文件,对其内容进行一些修改,然后将结果作为PR推送到一个新的、独立的分支。我已经弄清楚了这里的大部分步骤,但我陷入了解析提交文件内容部分。我已经尝试过get_item_content和......