擅长:python、mysql、java
<p>您可以将<code>__lt__</code>和<code>__gt__</code>方法添加到<code>Foo</code>类(小于和大于)中,这些类将<code>Foo</code>对象计算为小于或大于整数,这在未提供键函数时由<code>sort</code>使用</p>
<pre><code>class Foo():
def __lt__(self, other):
if isinstance(other, int):
return self._id < other
elif isinstance(other, self.__class__):
return self._id < other._id
def __gt__(self, other):
if isinstance(other, int):
return self._id > other
elif isinstance(other, self.__class__):
return self._id > other._id
</code></pre>