鱼C论坛

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

[已解决]课后作业43

[复制链接]
发表于 2020-11-19 10:24:28 | 显示全部楼层 |阅读模式

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

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

x
class Word(str):
'''存储单词的类,定义比较单词的几种方法'''

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

    def __gt__(self, other):
        return len(self) > len(other)
    def __lt__(self, other):
        return len(self) < len(other)
    def __ge__(self, other):
        return len(self) >= len(other)
    def __le__(self, other):
        return len(self) <= len(other)



弱弱的问一句,为什么要重写 __gt__,__lt__, __ge__, __le__ 这些魔法方法呢?
最佳答案
2020-11-19 10:33:49
本帖最后由 jackz007 于 2020-11-19 10:54 编辑

        Word 类继承自 str,如果不写这些魔法方法,当对象之间使用 > 、< 、>=、<= 进行比较的时候,就会缺省使用 str 类的魔法方法,这些魔法方法只考虑了字符串的字符编码,忽略了字符串的长度,而我们需要的是,只考虑字符串长度,无视字符串编码的比较结果,所以,就需要重写这些魔法方法。
        看看下面的例子就明白了
  1. >>> class Word(str):
  2.     def __new__(cls, word):
  3.         if ' ' in word:
  4.             print("Value contains spaces. Truncating to first space.")
  5.             word = word[:word.index(' ')]
  6.         return str.__new__(cls, word)

  7.     def __gt__(self, other):
  8.         return len(self) > len(other)
  9.     def __lt__(self, other):
  10.         return len(self) < len(other)
  11.     def __ge__(self, other):
  12.         return len(self) >= len(other)
  13.     def __le__(self, other):
  14.         return len(self) <= len(other)

  15. >>> a = Word('abc')
  16. >>> b = Word('b')
  17. >>> a > b
  18. True
  19. >>> a = 'abc'
  20. >>> b = 'b'
  21. >>> a > b
  22. False
  23. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-19 10:28:20 | 显示全部楼层
因为这些魔法方法的原本功能与这里想要实现的功能不一样
这些魔法方法原来的功能是比较字符串的大小,是从头到尾按照字符的ASCII码进行比较的,但是现在要实现的是按照字符串的长度进行比较大小,所以需要重写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-19 10:33:49 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-11-19 10:54 编辑

        Word 类继承自 str,如果不写这些魔法方法,当对象之间使用 > 、< 、>=、<= 进行比较的时候,就会缺省使用 str 类的魔法方法,这些魔法方法只考虑了字符串的字符编码,忽略了字符串的长度,而我们需要的是,只考虑字符串长度,无视字符串编码的比较结果,所以,就需要重写这些魔法方法。
        看看下面的例子就明白了
  1. >>> class Word(str):
  2.     def __new__(cls, word):
  3.         if ' ' in word:
  4.             print("Value contains spaces. Truncating to first space.")
  5.             word = word[:word.index(' ')]
  6.         return str.__new__(cls, word)

  7.     def __gt__(self, other):
  8.         return len(self) > len(other)
  9.     def __lt__(self, other):
  10.         return len(self) < len(other)
  11.     def __ge__(self, other):
  12.         return len(self) >= len(other)
  13.     def __le__(self, other):
  14.         return len(self) <= len(other)

  15. >>> a = Word('abc')
  16. >>> b = Word('b')
  17. >>> a > b
  18. True
  19. >>> a = 'abc'
  20. >>> b = 'b'
  21. >>> a > b
  22. False
  23. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-19 19:03:15 | 显示全部楼层
jackz007 发表于 2020-11-19 10:33
Word 类继承自 str,如果不写这些魔法方法,当对象之间使用 > 、< 、>=、

明白了!!谢谢!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-22 10:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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