47讲课上的摄氏度与华氏度转换案例
class Celsius:def __init__(self, value = 26.0):
self.value = float(value)
def _get_(self, instance, owner):
return self.value
def _set_(sel, instance, value):
self.value = float(value)
class Fahrenheit:
def _get_(self, instance, value):
return self.instance.cel * 1.8 + 32
def _set_(self, instance, value):
instance.cel = (float(value) - 32) / 1.8
class Temperture:
cel = Celsius()
fah = Fahrenheit()
idle中输入
temp = Temperture()
temp.cel
返回的是<__main__.Celsius object at 0x0000014E56173700>
而不是26 是什么原因呢 {:5_96:}已经解决啦 魔法方法的下横线长度少了 {:5_108:}
页:
[1]