[Python-Dev] redefining is (original) (raw)

Barry Warsaw barry at python.org
Fri Mar 19 14:39:41 EST 2004


On Fri, 2004-03-19 at 14:25, Michael Chermside wrote:

I hear you, but I disagree. There is a good reason why the default implementation of '==' compares by object identity.

I don't care much whether we use "if obj is None" rather than "if obj == None", since the PARTICULAR case of comparing with None is mostly just an idiom.

I think it's more than that. Python says there's only one None object, and when I'm testing for, e.g. the return value from dict.get(), I'm specifically testing that the object returned is that singleton object. By using identity test, I'm avoiding any possibility that someone slipped me a subversive (or buggy) object that happens to claim it's equal to None when it is not None.

When I use "if obj is None" I really, truly want an identity test. I don't think == is appropriate for that.

But if you have two different identity objects and want to compare them, then I would prefer

if currentCustomer == desiredCustomer: to if currentCustomer is desiredCustomer:

It depends on what you want compared. If you're happy with any old arbitrary claims of equality that the objects can make about themselves, then fine. If you're asking, "do I have the exact object that I want" then it's not.

However, either one works, and I won't gripe about your using 'is' if you prefer it. I WOULD gripe if you overrode eq to raise an exception in order to force me to use 'is' not '==' on your objects (not that you ever suggested doing so).

Hey, we're all consenting adults here (you high school kids, look away! :).

-Barry



More information about the Python-Dev mailing list