What is the Python <> operator

后端 未结 2 1663
無奈伤痛
無奈伤痛 2021-01-20 12:19

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

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-20 12:44

    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.

提交回复
热议问题