python基础学习1-描述符
#!/usr/bin/env python # -*- coding:utf-8 -*- #描述符就是将某种特殊类型的类的实例指派给另一个类的属性 #特殊类型指 实现了 # __get__(self,instance,owner), # __set__(self,instance,value), # __delete__(self,instance)三个方法中至少一个 class MyDecriptor: #描述符类 def __get__(self, instance, owner): print("__get__",self, instance, owner) def __set__(self, instance, value): print("__set__",self, instance, value) def __delete__(self, instance): print("__delete__",self, instance) class Test: x=MyDecriptor() t1= Test() print(t1.x) t1.x="x-man" #=====》实例温度转换 根据设置的摄氏度换算出华氏度 class Celsius: def __init__(self,value=26): self.value=value def __get__(self, instance, owner): return self.value def __set__(self, instance, value): self.value = 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(30) fah=Fahrenheit() tem = Temperature() print(tem.cel) print(tem.fah)