dol*_*don 3 python class operator-keyword gt
class Node:
def __init__(self,a,b):
self._a=a
self._b=b
def __lt__(self,other):
return self._a<other._a
a=Node(1,2)
b=Node(0,4)
print(a>b)
Run Code Online (Sandbox Code Playgroud)
上面的代码显示 True。
class Node:
def __init__(self,a,b):
self._a=a
self._b=b
def __lt__(self,other):
return self._a<other._a
def __eq__(self,other):
return self._a==other._a
a=Node(1,2)
b=Node(0,4)
print(a>=b)
Run Code Online (Sandbox Code Playgroud)
上面的代码显示了 TypeError: 'Node' 和 'Node.js 实例之间不支持'<='。
为什么仅定义lt就可以进行 >(即gt)操作?为什么同时定义lt和eq会使 <= 不可能?
| 归档时间: |
|
| 查看次数: |
947 次 |
| 最近记录: |