鱼C论坛

 找回密码
 立即注册
查看: 1168|回复: 4

关于自己写的一个类的问题

[复制链接]
发表于 2019-7-14 17:35:00 | 显示全部楼层 |阅读模式

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

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

x
关于重写 字符串比较的方法:
代码如下:
  1. def New_str(str):
  2.     def __new__(cls,word):
  3.         if ' '  in word:
  4.             print('里面有空格 不合法')
  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)
复制代码


我之前自己写的 总是说不能两个无类型的参数之间比较,写了这个小甲鱼的课后答案的代码还是会出来类似问题

>>> a = New_str('asds')
>>> b = New_str('asdssds')
>>> a < b
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    a < b
TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'
>>>

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

使用道具 举报

 楼主| 发表于 2019-7-14 17:35:47 | 显示全部楼层
大佬能不能看一下 最后报错的原因是无类型之间不能比较 怎么会这样 我以为是程序问题 我重启程序依旧不行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-14 18:16:17 From FishC Mobile | 显示全部楼层
你的缩进
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-14 18:23:20 From FishC Mobile | 显示全部楼层
还有class
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-15 07:09:11 | 显示全部楼层
明白了 明白了 谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-16 21:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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