|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 legend818 于 2017-8-18 14:31 编辑
小甲鱼课时46魔法方法:描述符(Property的原理)视频 出现下面错误
- #描述符类
- 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()
复制代码
调用出现错误:(求指正)
>>> temp = Temperature
>>> temp.cel
26.0
>>> temp.cel = 30
>>> temp.fah
Traceback (most recent call last):
File "<pyshell#190>", line 1, in <module>
temp.fah
File "D:/python/零基础入门学习Python/第11章节/课时46魔法方法:描述符(Property的原理).py", line 14, in __get__
return instance.cel*1.8 +32
AttributeError: 'NoneType' object has no attribute 'cel'
>>>
2楼指导:
- >>> temp = Temperature()
- >>> temp.cel
- 26.0
- >>> temp.cel = 30
- >>> temp.cel
- 30.0
- >>> temp.fah = 112
- >>> temp.cel
- 44.44444444444444
复制代码
tt = Turle() #类对象实例化
- >>> t=Temperature()
- >>> t.cel=50
- >>> t.fah
- 122.0
- >>> t.fah=112
- >>> t.cel
- 44.44444444444444
- >>> t=Temperature
- >>> t.cel=50
- >>> t.fah
- Traceback (most recent call last):
- File "<pyshell#7>", line 1, in <module>
- t.fah
- File "C:\Users\Administrator\Desktop\1.py", line 14, in __get__
- return instance.cel*1.8 +32
- AttributeError: 'NoneType' object has no attribute 'cel'
- >>>
复制代码
|
|