Annoying behaviour of the != operator

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jun 8 19:18:45 EDT 2005


On Wed, 08 Jun 2005 11:01:27 -0700, Mahesh wrote:

> No, why should Python assume that if you use != without supplying a
> __ne__ that this is what you want? Without direction it will compare
> the two objects which is the default behavior.

Why should Python assume that != means "not is" instead of "not equal"?

That seems like an especially perverse choice given that the operator is
actually called "not equal".

> So, s != t is True because the ids of the two objects are different.
> The same applies to, for example s > t and s < t. Do you want Python to
> be smart and deduce that you want to compare one variable within the
> object if you don't create __gt__ and __lt__? I do not want Python to
> do that.

That is an incorrect analogy. The original poster doesn't want Python to
guess which attribute to do comparisons by. He wants "!=" to be
defined as "not equal" if not explicitly overridden with a __ne__ method.

If there are no comparison methods defined, then and only then does it
make sense for == and != to implicitly test object identity.

I'm all for the ability to override the default behaviour. But surely
sensible and intuitive defaults are important?


-- 
Steven





More information about the Python-list mailing list