作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这只是编写相同代码的两种方式吗?有什么我应该注意的功能差异吗?
>>> a = 'foo'
>>> if not a == 'bar':
... 'its not'
...
'its not'
>>> if a != 'bar':
... 'its not'
...
'its not'
最佳答案
在 python 中,要检查一个对象是否等于另一个对象,会调用特殊函数。调用__eq__
来检查==
,而调用__ne__
来检查!=
通常,一个对象可以定义__ne__
与__eq__
不同。
例如
class Junk(object):
def __ne__(self, other):
return False
def __eq__(self, other):
return False
j = Junk()
print not j == 1
print j != 1
这会产生:
True
False
然而,这将是特别邪恶的......你通常永远不必担心这一点。
关于python - : if not a == 'bar' and if a ! = 'bar' 之间有显着差异吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8514427/
我是一名优秀的程序员,十分优秀!