私は、Python 2のコードを使用する場合、それは私にエラーのpython 2とPython 3 __cmp__
class point:
def __init__(self,x,y):
self.x=x
self.y=y
def dispc(self):
return ('(' +str(self.x)+','+str(self.y)+')')
def __cmp__(self,other):
return ((self.x > other.x) and (self.y > other.y))
を与えるPythonの3ながら、それが正常に動作します............... .................................................. ...
[email protected]:~/Documents/Programs$ python3 -i classes.py
>>> p=point(2,3)
>>> q=point(3,4)
>>> p>q
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: point() > point()
>>>
[email protected]:~/Documents/Programs$ python -i classes.py
>>> p=point(2,3)
>>> q=point(3,4)
>>> p>q
False
>>>
........................................ ...........................
==
とのみで動作している間は、python 3ではand
にエラーがあります。
解決策をご提案ください。
PEP 8勧告は、すべての6つの豊富な比較を提供することです。簡単にするには、* functools.total_ordering *を使用してください。 –
__cmp__の代わりに__ltと__eq__を使用するにはどうすればよいですか? –
さて、「lt」はLess Thanの略です。 'self'が他のパラメータよりも小さいかどうかを示すブール値を返します。他は同様に動作します。私はあなたが詳細を理解することができると確信しています。 –