# # @File : # @Author: dianxiaoer # @Date : 2019/11/5 # @Desc : # __getattr__() 定义当用户试图访问一个不存在的属性时的行为 # __getattribute__() 定义当该类的属性被访问时的行为 # class C: # def __getattribute__(self, name): # print("getattribute") # return super().__getattribute__(name) # def __getattr__(self, name): # print("getattr") # def __setattr__(self, name, value): # print("setattr") # super().__setattr__(name,value) # def __delattr__(self, name): # print("delattr") # super().__delattr__(name) # # c = C() # print(c.x) # print("*"*10) # c.x = 1 # print("*"*10) # del c.x # print(c.x) # 死循环陷阱 # class Rectangle: # def __init__(self, width = 0, height = 0): # self.width = width # self.height = height # # def __setattr__(self, name, value): # if name == "square": # self.width = value # self.height = value # else: # self.name = value # # def getArea(self): # return self.width*self.height # # r1 = Rectangle(4,5) # 死循环陷阱改进 # class Rectangle: # def __init__(self, width=0, height=0): # self.width = width # self.height = height # # def __setattr__(self, name, value): # if name == "square": # self.width = value # self.height = value # else: # # super().__setattr__(name,value) 推荐方式 # self.__dict__[name] = value # # def getArea(self): # return self.width * self.height # # # r1 = Rectangle(4, 5) # print(r1.getArea()) # print("*" * 10) # r1.square = 10 # print(r1.height) # print(r1.getArea()) # print(r1.__dict__) # 描述符:将某种特殊类型的类的实例指派给另一个类的属性 # __get__() # 用于访问属性,它返回属性的值 # __set__() # 将属性在分配操作中使用,不返回任何的值 # __delete__() # 控制删除操作,不返回任何内容 # class MyDecriptor: # def __get__(self, instance, owner): # print("getting……",self,instance,owner) # # def __set__(self, instance, value): # print("setting……",self,instance,value) # # def __delete__(self, instance): # print("delete……",self,instance) # # class Test: # x = MyDecriptor() # # test = Test() # print(test.x) # print(test) # print("*"*10) # test.x = "diaoxiaoer" # print("*"*10) # del test.x # class MyProperty: # def __init__(self,fget=None,fset=None,fdel=None): # self.fget = fget # self.fset = fset # self.fdel = fdel # # def __get__(self, instance, owner): # return self.fget(instance) # # def __set__(self, instance, value): # self.fset(instance,value) # # def __delete__(self, instance): # self.fdel(instance) # # class C: # def __init__(self): # self.x = None # # def getX(self): # return self._x # # def setX(self,value): # self._x = value # # def delX(self,instance): # del self._x # # x = MyProperty(getX,setX,delX) # # c = C() # c.x = "dianxiaoer" # print(c.x) ############################################################ # class Celsius: # def __init__(self,value = 26.0): # self.value = value # # def __get__(self, instance, owner): # return self.value # # def __set__(self, instance, value): # self.value = float(value) # # class Fahrenheit: # def __get__(self, instance, owner): # return instance.cel*1.8 + 32 # # def __set__(self, instance, value): # instance.cel = float(value-32)/1.8 # # class Temperature: # cel = Celsius() # fah = Fahrenheit() # # temp = Temperature() # print(temp.cel) # print("**********************************") # temp.cel = 30 # print(temp.fah)
魔术方法之描述符
最新推荐文章于 2021-02-21 01:07:35 发布