51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 874|回复: 0
打印 上一主题 下一主题

[python] 教你如果在Python 装饰一个类

[复制链接]
  • TA的每日心情
    无聊
    昨天 08:59
  • 签到天数: 946 天

    连续签到: 5 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-9-9 14:16:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    它使用额外的 Python 语句修改旧对象并返回相同的引用。
      例如,考虑下面的类,它有两个方法:__init__和 display。__init__方法在显示输出名称时初始化名称变量:
    1. class Student:
    2.     def __init__(self, name):
    3.         self.name = name
    4.     def display(self):
    5.         print('Name:', self.name)
    复制代码
    要在 Python 中装饰这个类,我们可以向该类添加新方法或修改现有方法,或者两者都做。
      此外,在 Python 中有两种方法可以做到这一点,要么使用函数装饰器,要么使用类装饰器。
      让我们一个一个看下例子。
      使用函数装饰器装饰类
      要使用函数装饰器来装饰类,接受类作为参数,修改其代码并在最后返回类。
    1. def mydecorator(student):
    2.     #define a new display method
    3.     def newdisplay(self):
    4.         print('Name: ', self.name)
    5.         print('Subject: Programming')
    6.     #replace the display with newdisplay
    7.     student.display = newdisplay
    8.     #return the modified student
    9.     return student
    10.   @mydecorator
    11.   class Student:
    12.     def __init__(self, name):
    13.         self.name = name
    14.     def display(self):
    15.         print('Name:', self.name)
    16.   obj = Student('Pencil Programmer')
    17.   obj.display()
    18.   '''
    19.   Name: Pencil Programmer
    20.   Subject: Programming
    21.   '''
    复制代码
    如果类中不存在 display 方法,则 newdisplay 将作为 display 方法添加到类中。
      由于类的引用在装饰器函数中是可用的,我们除了修改现有方法外,还可以为类添加新的属性和方法
      使用类装饰器装饰类
      要使用类装饰器装饰类,接受类的引用作为参数(在装饰器的__init__方法中),在 __call__方法中修改其代码,最后返回修改后的类的实例。
    1. class Mydecorator:
    2.     #accept the class as argument
    3.     def __init__(self, student):
    4.         self.student = student
    5.     #accept the class's __init__ method arguments
    6.     def __call__(self, name):
    7.         #define a new display method
    8.         def newdisplay(self):
    9.             print('Name: ', self.name)
    10.             print('Subject: Python')
    11.         #replace display with newdisplay
    12.         self.student.display = newdisplay
    13.         #return the instance of the class
    14.         obj = self.student(name)
    15.         return obj
    16.   @Mydecorator
    17.   class Student:
    18.     def __init__(self, name):
    19.         self.name = name
    20.     def display(self):
    21.         print('Name: ', self.name)
    22.   obj = Student('Pencil Programmer')
    23.   obj.display()
    24.   '''
    25.   Name: Pencil Programmer
    26.   Subject: Python
    27.   '''
    复制代码
    这里唯一的区别是我们返回的是对象的引用而不是类引用。



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-11 04:03 , Processed in 0.062438 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表