在与两个整数进行比较的情况下,它们是相同的.更喜欢!=更像pythonic.
如果任一操作数是自定义类的实例,则结果可能不同.自定义类可以独立覆盖==和!=运算符(即使是疯狂的结果)
来自LHS:
>>> class A:
... def __eq__(self, other):
... return False
... def __ne__(self, other):
... return False
...
>>> a = A()
>>> a != 5
False
>>> not a == 5
True
来自RHS:
>>> class R(str):
... def __eq__(self, other):
... return False
... def __ne__(self, other):
... return False
...
>>> r = R()
>>> 'spam' != r
False
>>> not 'spam' == r
True
最后
以上就是强健大象最近收集整理的关于python not是什么意思_python – “not ==”和“!=”之间有什么区别?的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复