描述符使用

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

 

posted @ 2018-11-15 14:38  986428528  阅读(101)  评论(0)    收藏  举报