鱼C论坛

 找回密码
 立即注册
查看: 1559|回复: 1

[已解决]课时46魔法方法:描述符(Property的原理)

[复制链接]
发表于 2017-8-18 13:24:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 legend818 于 2017-8-18 14:31 编辑

小甲鱼课时46魔法方法:描述符(Property的原理)视频 出现下面错误

  1. #描述符类
  2. class Celsius:
  3.     def __init__(self, value=26.0):
  4.         self.value = float(value)

  5.     def __get__(self,instance, owner):
  6.         return self.value

  7.     def __set__(self, instance, value):
  8.         self.value = float(value)

  9. class Fahrenheit:
  10.     def __get__(self, instance, owner):
  11.         return instance.cel*1.8 +32

  12.     def __set__(self, instance, value):
  13.         instance.cel = (float(value) -32)/1.8


  14. class Temperature:
  15.     cel = Celsius()
  16.     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楼指导:
  1. >>> temp = Temperature()
  2. >>> temp.cel
  3. 26.0
  4. >>> temp.cel = 30
  5. >>> temp.cel
  6. 30.0
  7. >>> temp.fah = 112
  8. >>> temp.cel
  9. 44.44444444444444
复制代码
最佳答案
2017-8-18 13:40:53


tt = Turle() #类对象实例化

  1. >>> t=Temperature()
  2. >>> t.cel=50
  3. >>> t.fah
  4. 122.0
  5. >>> t.fah=112
  6. >>> t.cel
  7. 44.44444444444444
  8. >>> t=Temperature
  9. >>> t.cel=50
  10. >>> t.fah
  11. Traceback (most recent call last):
  12.   File "<pyshell#7>", line 1, in <module>
  13.     t.fah
  14.   File "C:\Users\Administrator\Desktop\1.py", line 14, in __get__
  15.     return instance.cel*1.8 +32
  16. AttributeError: 'NoneType' object has no attribute 'cel'
  17. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-18 13:40:53 | 显示全部楼层    本楼为最佳答案   


tt = Turle() #类对象实例化

  1. >>> t=Temperature()
  2. >>> t.cel=50
  3. >>> t.fah
  4. 122.0
  5. >>> t.fah=112
  6. >>> t.cel
  7. 44.44444444444444
  8. >>> t=Temperature
  9. >>> t.cel=50
  10. >>> t.fah
  11. Traceback (most recent call last):
  12.   File "<pyshell#7>", line 1, in <module>
  13.     t.fah
  14.   File "C:\Users\Administrator\Desktop\1.py", line 14, in __get__
  15.     return instance.cel*1.8 +32
  16. AttributeError: 'NoneType' object has no attribute 'cel'
  17. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-3 18:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表