Python描述符分类
数据描述符:实现了__get__ 和 __set__ 两种方法的描述符
非数据描述符:只实现了__get__ 一种方法的描述符
Python描述符实例
# 数据描述符 class DataDes: def __init__(self, default=0): self._score = default def __set__(self, instance, value): self._score = value def __get__(self, instance, owner): print("访问数据描述符里的 __get__") return self._score # 非数据描述符 class NoDataDes: def __init__(self, default=0): self._score = default def __get__(self, instance, owner): print("访问非数据描述符里的 __get__") return self._score class Student: math = DataDes(0) chinese = NoDataDes(0) def __init__(self, name, math, chinese): self.name = name self.math = math self.chinese = chinese def __getattribute__(self, item): print("调用 __getattribute__") return super(Student, self).__getattribute__(item) def __repr__(self): return "<Student: {}, math:{}, chinese: {},>".format( self.name, self.math, self.chinese)
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试