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
    


  • 相关阅读:
    菜根谭#188
    菜根谭#187
    Single value range only allowed in SystemVerilog
    LUTs, Flip-Flop, Slice
    FPGA 的 RAM 的 区别
    GPU core clock, shader clock ???
    更改Mac的Terminal 格式
    GPU share memory 的应用 (主要内容转载)
    Mac text edit & pdf reader
    Programming Font
  • 原文地址:https://www.cnblogs.com/keye/p/15718182.html
Copyright © 2011-2022 走看看