Annoying behaviour of the != operator
steve at REMOVETHIScyber.com.au
Thu Jun 9 01:18:45 CEST 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?
More information about the Python-list