描述符使用
Posted wuxi9864
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了描述符使用相关的知识,希望对你有一定的参考价值。
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
以上是关于描述符使用的主要内容,如果未能解决你的问题,请参考以下文章
编写一个程序, 将 a.txt 文件中的单词与 b.txt 文件中的 单词交替合并到 c.txt 文件中, a.txt 文件中的单词用回车符 分隔, b.txt 文件中用回车或空格进行分隔。(代码片段