I've got a head scratchier and it seems I'm not the only one, but is there really no solution? I find that hard to believe!
So the question is why can't I call int.__eq__
with 2 operators or i.__eq__
with one? How can I use __eq__
(and the other comparison operators) for a per item comparison for a sequence of ints?
Here's a dump from python2.7.17:
>>> i = 0
>>> type(i)
<type 'int'>
>>> i.__eq__(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__eq__'
>>> type(i).__eq__(i, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected 1 arguments, got 2
>>> type(i).__eq__(0)
NotImplemented
But my dumo from python3.6.9 behaves itself:
>>> i = 0
>>> type(i)
<class 'int'>
>>> i.__eq__(0)
True
>>> type(i).__eq__(i, 0)
True
>>> type(i).__eq__(0) # this is not expected to work, but just for the sake of voodoo.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected 1 arguments, got 0
I know python2 is no longer supported but there are a few applications that only use python2 and I would like to make my code backwards compatible anyway.
So anyone out there have a solutuon for hacking the comparison magic operator methods as function calls in python2? I am sure there must be some work around.
It seems there is some information on this. I just read that python2 falls back to using cmp in some cases, while in python3 there is no cmp (or so I read). So I guess the thing to do is not use eq and ne but instead use cmp but I love some additional perspective on this