zoukankan      html  css  js  c++  java
  • 描述符使用

    class MyProperty:
        def __init__(self,func):
            self.func=func            #self.func=area
        def __get__(self, instance, owner):
            print("in __get__")
            if instance is None:
                return self
            return self.func(instance)  #area(instance
    
    class Room:
        def __init__(self,name,width,length):
            self.name=name
            self.width=width
            self.length=length
        @MyProperty   #area=MyProperty(area)
        def area(self):
            return self.width * self.length
    
    r1=Room('wes',10,20)
    print(Room.__dict__)  #'area': <__main__.MyProperty object at 0x01E233D0>,
    print(r1.__dict__)  #{'name': 'wes', 'width': 10, 'length': 20}
    # print(r1.area())
    print(r1.area)        #实现property
    class MyProperty: #没有实现__set__是非数据描述符
        def __init__(self,func):
            self.func=func            #self.func=area
        def __get__(self, instance, owner):
            print("in __get__")
            if instance is None:
                return self
            val= self.func(instance)  #area(instance
            setattr(instance,self.func.__name__,val)
            return val
    class Room:
        def __init__(self,name,width,length):
            self.name=name
            self.width=width
            self.length=length
        @MyProperty   #area=MyProperty(area)
        def area(self):
            return self.width * self.length
    
    r1=Room('wes',10,20)
    print(Room.__dict__)  #'area': <__main__.MyProperty object at 0x01E233D0>,
    print(r1.__dict__)  #{'name': 'wes', 'width': 10, 'length': 20}
    # print(r1.area())
    print(r1.area)
    print(r1.__dict__)
    print(r1.area)
    #先从自己的属性字典找,没有再去类的中找,然后出发了area的__get__方法
    print(r1.area)

    #先从自己的属性字典找,找到了,是上次计算的结果,这样就不用每执行一次都去计算
    class MyProperty: #实现__set__是数据描述符,优先级高于实例属性
        def __init__(self,func):
            self.func=func            #self.func=area
        def __get__(self, instance, owner):
            print("in __get__")
            if instance is None:
                return self
            val= self.func(instance)  #area(instance
            setattr(instance,self.func.__name__,val)
            return val
        def __set__(self, instance, value):             #__set__
            pass
    class Room:
        def __init__(self,name,width,length):
            self.name=name
            self.width=width
            self.length=length
        @MyProperty   #area=MyProperty(area)
        def area(self):
            return self.width * self.length
    
    r1=Room('wes',10,20)
    print(Room.__dict__)  #'area': <__main__.MyProperty object at 0x01E233D0>,
    print(r1.__dict__)  #{'name': 'wes', 'width': 10, 'length': 20}
    # print(r1.area())
    print(r1.area)
    print(r1.__dict__)
    print(r1.area)
    print(r1.area)
    
    {'__module__': '__main__', '__init__': <function Room.__init__ at 0x00805300>, 'area': <__main__.MyProperty object at 0x008033B0>, '__dict__': <attribute '__dict__' of 'Room' objects>, '__weakref__': <attribute '__weakref__' of 'Room' objects>, '__doc__': None}
    {'name': 'wes', 'width': 10, 'length': 20}
    in __get__
    200
    {'name': 'wes', 'width': 10, 'length': 20}
    in __get__
    200
    in __get__
    200
  • 相关阅读:
    VLC播放器web插件接口(Part1)
    视频监控/存储系统设计要点
    CVR并发写入测试
    Darwin Streaming Server性能测试报告
    用Red5搭建支持WEB播放的实时监控视频
    RTSP协议-中文定义
    网格最短路径算法(Dijkstra & Fast Marching)
    三维网格精简算法(Quadric Error Metrics)附源码
    三维网格细分算法(Catmull-Clark subdivision & Loop subdivision)附源码
    网格测地线算法(Geodesics in Heat)附源码
  • 原文地址:https://www.cnblogs.com/wuxi9864/p/9963372.html
Copyright © 2011-2022 走看看