[英]Why does Python have an __ne__ operator method instead of just __eq__?
Some libraries do fancy things and don't return a bool from these operations. 有些图书馆做了很多东西,并没有从这些操作返回一个bool。 For example, with numpy:
例如,使用numpy:
>>> import numpy as np
>>> np.array([1,2,5,4,3,4,5,4,4])==4
array([False, False, False, True, False, True, False, True, True], dtype=bool)
>>> np.array([1,2,5,4,3,4,5,4,4])!=4
array([ True, True, True, False, True, False, True, False, False], dtype=bool)
When you compare an array to a single value or another array you get back an array of bools of the results of comparing the corresponding elements. 当您将数组与单个值或另一个数组进行比较时,您会得到一个比较相应元素的结果的bool数组。 You couldn't do this if
x!=y
was simply equivalent to not (x==y)
. 如果
x!=y
简单地等于not (x==y)
则无法执行此操作。
SQLAlchemy is a great example. SQLAlchemy就是一个很好的例子。 For the uninitiated, SQLAlchemy is a ORM and uses Python expression to generate SQL statements.
对于初学者,SQLAlchemy是一个ORM并使用Python表达式来生成SQL语句。 In a expression such as
在诸如的表达中
meta.Session.query(model.Theme).filter(model.Theme.id == model.Vote.post_id)
the model.Theme.id == model.VoteWarn.post_id
does not return a boolean, but a object that eventually produces a SQL query like WHERE theme.id = vote.post_id
. model.Theme.id == model.VoteWarn.post_id
不返回布尔值,而是返回最终产生类似WHERE theme.id = vote.post_id
的SQL查询的对象。 The inverse would produce something like WHERE theme.id <> vote.post_id
so both methods need to be defined. 逆会产生类似
WHERE theme.id <> vote.post_id
因此需要定义两种方法。
More generally, in many valued logic systems, equals
and not equals
are not necessarily exact inverses of each other. 更一般地,在许多有价值的逻辑系统中,
equals
not equals
不一定是彼此的精确逆。
The obvious example is SQL where True == True
, False == False
and Null != Null
. 显而易见的例子是SQL,其中
True == True
, False == False
, Null != Null
。 Although I don't know if there are any specific Python examples I can imagine it being implemented in places. 虽然我不知道是否有任何特定的Python示例,但我可以想象它是在某些地方实现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.