Python not equal operator

我怕爱的太早我们不能终老 提交于 2019-11-26 11:38:19

问题


I come from a c style languages, so I am natural in using != as not equal, but when I came to Python, from the documentation I read, I learned that for this purpose the <> operator is used.

Recently, I have seen a lot of code using !=, so my question is if one of them is preferred over the other or is one of them deprecated.

Also, I would like to know if there is any difference between them.


回答1:


Python 2 supports both, in python 3 the <> operator has been removed.

There is no difference between the two, but != is the preferred form.




回答2:


From the official docs you linked

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

I believe the rationale for originally accepting <> was that it looked more natural for someone coming from a mathematical background than the common C-style != operator.




回答3:


I don't know what documentation you read, but I'm not aware of any that recommends <> over !=. PEP8, the main style guide, doesn't mention any such recommendation.




回答4:


Just for the record,<> has been obsolete since at least as early as version 1.4, which was released in October 1996.



来源:https://stackoverflow.com/questions/12351221/python-not-equal-operator

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