鱼C论坛

 找回密码
 立即注册
查看: 3375|回复: 3

[已解决]Python,看了答案不会 run...

[复制链接]
发表于 2021-3-16 15:28:06 | 显示全部楼层 |阅读模式

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

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

x
原题:
定义一个单词(Word)类继承自字符串,重写比较操作符,当两个 Word 类对象进行比较时,根据单词的长度来进行比较大小。
Fish C的答案:
  1. class Word(str):
  2. '''存储单词的类,定义比较单词的几种方法'''

  3.     def __new__(cls, word):
  4.         # 注意我们必须要用到 __new__ 方法,因为 str 是不可变类型
  5.         # 所以我们必须在创建的时候将它初始化
  6.         if ' ' in word:
  7.             print "Value contains spaces. Truncating to first space."
  8.             word = word[:word.index(' ')] #单词是第一个空格之前的所有字符
  9.         return str.__new__(cls, word)

  10.     def __gt__(self, other):
  11.         return len(self) > len(other)
  12.     def __lt__(self, other):
  13.         return len(self) < len(other)
  14.     def __ge__(self, other):
  15.         return len(self) >= len(other)
  16.     def __le__(self, other):
  17.         return len(self) <= len(other)
复制代码


我运行不成功:
  1. ================== RESTART: C:/Users/user/Desktop/20210312a.py =================
  2. >>> __gt__(apple, cat)
  3. Traceback (most recent call last):
  4.   File "<pyshell#26>", line 1, in <module>
  5.     __gt__(apple, cat)
  6. NameError: name '__gt__' is not defined
  7. >>> Word(apple, cat)
  8. Traceback (most recent call last):
  9.   File "<pyshell#27>", line 1, in <module>
  10.     Word(apple, cat)
  11. NameError: name 'apple' is not defined
  12. >>> __gt__.Word(apple, cat)
  13. Traceback (most recent call last):
  14.   File "<pyshell#28>", line 1, in <module>
  15.     __gt__.Word(apple, cat)
  16. NameError: name '__gt__' is not defined
  17. >>>
复制代码

怎么使用这个程序呀?我晕了。
最佳答案
2021-3-16 15:57:08
本帖最后由 jackz007 于 2021-3-16 16:01 编辑

        楼主,定义的魔法方法供类实例在使用 >、>= <、<= 进行比较的时候使用,一般不直接调用。
  1. >>> class Word(str):
  2.     '''存储单词的类,定义比较单词的几种方法'''

  3.     def __new__(cls, word):
  4.         # 注意我们必须要用到 __new__ 方法,因为 str 是不可变类型
  5.         # 所以我们必须在创建的时候将它初始化
  6.         if ' ' in word:
  7.             print("Value contains spaces. Truncating to first space.")
  8.             word = word[:word.index(' ')] #单词是第一个空格之前的所有字符
  9.         return str.__new__(cls, word)

  10.     def __gt__(self, other):
  11.         return len(self) > len(other)
  12.     def __lt__(self, other):
  13.         return len(self) < len(other)
  14.     def __ge__(self, other):
  15.         return len(self) >= len(other)
  16.     def __le__(self, other):
  17.         return len(self) <= len(other)

  18. >>> a = Word('ABC')
  19. >>> b = Word('1234')
  20. >>> a > b
  21. False
  22. >>> a <= b
  23. True
  24. >>> a < b
  25. True
  26. >>>
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-16 15:57:08 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-3-16 16:01 编辑

        楼主,定义的魔法方法供类实例在使用 >、>= <、<= 进行比较的时候使用,一般不直接调用。
  1. >>> class Word(str):
  2.     '''存储单词的类,定义比较单词的几种方法'''

  3.     def __new__(cls, word):
  4.         # 注意我们必须要用到 __new__ 方法,因为 str 是不可变类型
  5.         # 所以我们必须在创建的时候将它初始化
  6.         if ' ' in word:
  7.             print("Value contains spaces. Truncating to first space.")
  8.             word = word[:word.index(' ')] #单词是第一个空格之前的所有字符
  9.         return str.__new__(cls, word)

  10.     def __gt__(self, other):
  11.         return len(self) > len(other)
  12.     def __lt__(self, other):
  13.         return len(self) < len(other)
  14.     def __ge__(self, other):
  15.         return len(self) >= len(other)
  16.     def __le__(self, other):
  17.         return len(self) <= len(other)

  18. >>> a = Word('ABC')
  19. >>> b = Word('1234')
  20. >>> a > b
  21. False
  22. >>> a <= b
  23. True
  24. >>> a < b
  25. True
  26. >>>
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-16 16:00:51 | 显示全部楼层

谢谢。看似非常简单,自己写,就是被卡住。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-17 23:09:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-7 10:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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