What is the Python <> operator

*爱你&永不变心* 提交于 2019-12-04 04:23:56

问题


What exactly is the <> operator in Python, and why is it undocumented (as far as I can tell)?

Is it the same as != or is not?


回答1:


In Python 2.x, <> is the same as != (i.e. "not equal to", rather than is not which is "not identical to"), but the latter is preferred:

The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent.

In 3.x, <> has been removed and only != exists.




回答2:


It is documented, but you're not supposed to use it. Your guess about it being equivalent to != is correct. Quoting the Python 2 documentation:

!= can also be written <>, but this is an obsolete usage kept for backwards compatibility only. New code should always use !=.

Then as part of the general cleanup of Python 3, the operator was removed entirely:

Removed <> (use != instead).


Historical note

It goes back quite a long way; at least as far as Python 1.4. I found an entry in the old docs:

<> and != are alternate spellings for the same operator. (I couldn't choose between ABC and C! :-)

The docs started recommending != with Python 1.5.2p2.



来源:https://stackoverflow.com/questions/29238871/what-is-the-python-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!