I was reviewing some code today and saw what I figured was a typo:
if foo <> bar:
# do stuff
However, I'm surprised to see that Python2 appears to treat <>
as valid:
% python -c 'print 1<>2'
True
Additionally, I can't discern what causes <>
to return True
vs False
:
In [16]: 1 <> 2
Out[16]: True
In [17]: 2 <> 1
Out[17]: True
In [18]: 1 <> None
Out[18]: True
In [19]: 1 <> "foo"
Out[19]: True
In [20]: None <> 1
Out[20]: True
In [21]: None <> None
Out[21]: False
In [22]: False <> 1
Out[22]: True
One of my guesses was that Python was using both the <
and >
operators and I was falling victim to some boolean casting shenanigans, but that doesn't appear to be the case as < >
isn't valid and neither is ><
:
In [29]: 1 >< 2
File "<ipython-input-29-0e35e3b94016>", line 1
1 >< 2
^
SyntaxError: invalid syntax
In [30]: 1 < > 2
File "<ipython-input-30-8f2ba3fa3c6c>", line 1
1 < > 2
^
SyntaxError: invalid syntax
Finally, Python 3 chokes on this "operator" (or whatever it should be called):
% python3 -c 'print(1<>2)'
File "<string>", line 1
print(1<>2)
^
SyntaxError: invalid syntax
So, what gives? What is <>
doing? What is it? Why is it invalid in Python 3?