为什么Python有__ne__运算符方法而不是__eq__? [英] Why does Python have an __ne__ operator method instead of just __eq__?

查看:109
本文介绍了为什么Python有__ne__运算符方法而不是__eq__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处的答案为您提供了一些有用的参考我想让__ne__返回的内容不只是__eq__的逻辑逆,但是我无法想象任何这种情况.有例子吗?

The answer here gives a handwaving reference to cases where you'd want __ne__ to return something other than just the logical inverse of __eq__, but I can't imagine any such case. Any examples?

推荐答案

SQLAlchemy是一个很好的例子.对于未启动的用户,SQLAlchemy是一个ORM,并使用Python表达式生成SQL语句.在诸如

SQLAlchemy is a great example. For the uninitiated, SQLAlchemy is a ORM and uses Python expression to generate SQL statements. In a expression such as

meta.Session.query(model.Theme).filter(model.Theme.id == model.Vote.post_id)

model.Theme.id == model.VoteWarn.post_id不会返回布尔值,而是一个最终产生SQL查询的对象,例如WHERE theme.id = vote.post_id.反之会产生类似WHERE theme.id <> 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. The inverse would produce something like WHERE theme.id <> vote.post_id so both methods need to be defined.

这篇关于为什么Python有__ne__运算符方法而不是__eq__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆