python - 将类与无进行比较
问题描述
我有一个简单的情况,我正在尝试对包含混合类实例和无的列表进行排序。我已经实现了该类的lt方法,但仍然出现错误:
TypeError: '<' not supported between instances of 'NoneType' and 'test_class
这是我目前的做法:
class test_class:
def __init__(self, num):
self.num = num
def __lt__(self, other):
if other is None:
return False
else:
return self.num < other.num
def __eq__(self, other):
if other is None:
return False
else:
return self.num == other.num
tc1 = test_class(1)
tc2 = test_class(2)
sorted([tc1, tc2, None])
...产生上述错误。谁能指出我做错了什么?在编程语言以常识方式工作的某种理想化现实中,我会认为“如果其他是无”位应该处理与无的比较。
提前致谢!
解决方案
请注意,您的错误不是
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'test_class' and 'NoneType'
但反而
...between instances of 'NoneType' and 'test_class'
排序很重要。None
没有实现__lt__
知道如何与 a 进行比较的方法test_class
。然而,Python 足够聪明,可以__gt__
在这种情况下使用其他类。
class TestClass(object):
def __init__(self, num):
self.num = num
def __lt__(self, other):
if other is None:
return False
else:
return self.num < other.num
def __gt__(self, other):
if other is None:
return True
return self.num > other.num
def __eq__(self, other):
if other is None:
return False
else:
return self.num == other.num
更重要的是,functools.total_ordering
可以用来装饰你的类,所以你只需要定义, ,中的__eq__
一个__lt__
,其余的将为你自动生成。__le__
__gt__
__ge__
import functools
@functools.total_ordering
class TestClass(object):
def __init__(self, num):
self.num = num
def __lt__(self, other):
if other is None: return False
return self.num < other.num
def __eq__(self, other):
return isinstance(other, type(self)) and self.num == other.num
# I refactored this to be a little nicer
现在TestClass()
就像__gt__
,__ge__
和__le__
都已定义一样,即使您只需要定义一个。
推荐阅读
- angular - Khronos 语言环境错误:请在使用前加载语言环境“en”
- xcode - Mac App Store 上的 Xcode 无法安装,显示磁盘空间不足
- java - 在特定模式上编号
- javascript - 如何将带逗号的字符串转换为数字?
- php - 将数组传递给 in_array()
- r - 如何更改官员中的全局字体
- python - 带有 IntegrityError 异常的 Django 表单
- java - 如何根据另一个 ArrayList 中的值显示 ArrayList 中的特定项目?
- python - 在烧瓶中打开文件时出现异常
- python - 对 CSV 文件进行排序以在 Python 程序中读取