|
发表于 2021-5-9 00:55:36
|
显示全部楼层
__get__(), __set__(), __delete__() 这些特殊方法,用在 “描述器” Descriptors 。
你这里代码,似乎有点问题,我暂时知道怎么用,还没完全整明白。。
- class Celsius: # 可以理解为:Temperature温度类下,可以有 Celsius摄氏度类,这样一个对温度的描述,也可以有华氏度那样的一种描述。
- def __init__(self, value=0):
- self.value = value
-
- def __get__(self, instance, owner): # 本例子中,instance是指Temperature类的一个实例化对象,owner是Temperture类本身。
- return self.value
- def __set__(self, instance, value): # value 为 要设置的温度数。 当你使用 instance.cel=value 或 owner.cel=value 时,就会调用该__set__()
- # 具体到本例,即 temp.cel = 100 或 Temperature.cel = 300
- self.value = value
- class Fahrenheit: # Temperature温度类下,用 Fahrenheit华氏度,一种描述。
- def __get__(self, instance, owner): # 当 使用 temp.fah 时,就会调用该 __get__
- return round(owner.cel * 1.8 + 32, 2) # 获取摄氏度,并换算成 华氏度,保留两位小数,然后返回
- # return instance.cel * 1.8 + 32 # 存疑
- # 想想:这里为什么不能使用instance.cel,而要用owner.cel ?
- # 如果用instance.cel 会报错什么错? 为什么会报错这个错?
- # 。。我也没整明白
-
- def __set__(self, instance, value): # 当 temp.fah = xx 时,会调用该__set__
- instance.cel = round((value - 32) / 1.8, 2) # 换算成摄氏度,并赋值给cel变量( 实例化时,即 temp.cel = ...),会调用 Celsius中的__get__()
-
- class Temperature: # 定义一个温度类,针对它,可以有两种描述 --> 两种描述类,摄氏度类 和 华氏度类。
- cel = Celsius() # Celsius类 是Temperature类的一个描述类,因为其使用了 __get__() 或 __set__()
- fah = Fahrenheit() # Fahrenheit类,也是Temperature类的一个装饰类,其owner就是Temperture类
- temp = Temperature() # 实例化
- # __get__()的调用 之 Instance Binding 实例绑定:
- print(temp.cel) # 通过owner类 的实例化对象来调用, 等价于 type(temp).__dict__['cel'].__get__(temp, type(temp)).
- # print(type(temp).__dict__['cel'].__get__(temp, type(temp)))
- print(temp.fah)
- # print(type(temp).__dict__['fah'].__get__(temp, type(temp)))
- # __get__()的调用 之 Class Binding 类绑定:
- print(Temperature.cel) # 也可通过owner来调用
- print(Temperature.fah)
- # __set__()的调用
- temp.cel = 100
- print(f"100 ℃ = {temp.fah} ℉")
- print(f"100 ℃ = {Temperature.fah} ℉")
- print(type(temp).__dict__['fah'].__get__(temp, type(temp)))
- temp.fah = 200
- print(f"{temp.fah} ℉ = " + str(temp.cel) + " ℃") # 注意:想想为什么 temp.fah = 200 的200会变成 199.99
- print(f"{Temperature.fah} ℉ = " + str(temp.cel) + " ℃")
- Temperature.fah = 32 # 但不能这样赋值。否则将导致fah不再指向Fahrenheit描述类,而是直接指向 200这个值。
- print(f"{temp.fah} ℉ = " + str(temp.cel) + " ℃ ??")
复制代码 |
|