zoukankan      html  css  js  c++  java
  • Python 描述器

    一、问题

    创建有额外功能的属性。


    二、解决方案

    描述器的定义:

    class Integer:
        def __init__(self, name):
            self.name = name
            
        def __get__(self, instance, owner):
            if instance is None:
                return self
            else:
                return instance.__dict__[self.name]
            
        def __set__(self, instance, value):
            if not isinstance(value, int):
                raise TypeError('不是整型。')
            instance.__dict__[self.name] = value
            
        def __delete__(self, instance):
            del instance.__dict__[self.name]
    

    描述器是一个类,可以实现三个核心的属性访问__get__()__set__()__delete__()



    描述器的使用:将描述器的实例化作为类属性放到类的定义中。

    class Point:
        x = Integer('x')
        y = Integer('y')
    
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
    p = Point(2, 3)
    print(p.x)
    
    p.y = 5
    p.x = 2.3
    

    输出:

    2
    
    TypeError: 不是整型。
    

    描述器的每个方法接受一个实例。

    为实现请求操作,会相应的操作实例底层字典(__dict__属性)。

    self.name属性存储了在实例字典中被实际用到的key。



    三、讨论

    描述器可实现@classmethod@staticmethod@property__slots__

    描述器只能在类级别被定义,不能为每个实例单独定义。

    class Point:
        def __init__(self, x, y):
            self.x = Integer('x')   # 用法错误,必须是一个类变量
            self.y = Integer('y')
            self.x = x
            self.y = y
    


  • 相关阅读:
    【bzoj1036】【ZJOI2008】树的统计
    AE基础(8)PageLayout属性设置和添加元素
    AE基础(7)布局控件与地图控件关联
    UtilityAction扩展
    UtilityAction
    AE基础(6)数据查询与选择
    NavigationAction
    LayerAction
    AE基础(5)鹰眼功能
    AE基础(4)画几何图形
  • 原文地址:https://www.cnblogs.com/keye/p/15718182.html
Copyright © 2011-2022 走看看