马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我抄了一段代码,报错了,为何呀?class Celsius():
def __init__(self, value = 26.0):
self.value = float(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()
报错结果:================== RESTART: C:/Users/user/Desktop/20210322a.py =================
>>> Temperature.cel
26.0
>>> Temperature.fah
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
Temperature.fah
File "C:/Users/user/Desktop/20210322a.py", line 13, in __get__
return instance.cel * 1.8 + 32
AttributeError: 'NoneType' object has no attribute 'cel'
>>>
请教各位
Peteryo01223 发表于 2021-3-23 08:58
哦。应该怎么改代码呢?
class Celsius():
def __init__(self, value = 26.0):
self.value = float(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()
a=Temperature()
print(a.cel)
print(a.fah)
|