<var> is None vs. <var> == None
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Jan 24 00:34:16 EST 2009
On Fri, 23 Jan 2009 20:33:45 -0500, Steve Holden wrote:
> Steven D'Aprano wrote:
>> On Fri, 23 Jan 2009 14:58:34 -0500, Gerald Britton wrote:
>>
>>> Hi -- Some time ago I ran across a comment recommending using <var> is
>>> None instead of <var> == None (also <var> is not None, etc.)
>>
>> That entirely depends on whether you wish to test for something which
>> *is* None or something with *equals* None. Those two things have
>> different meanings.
>>
> No they don't, because the language *guarantees* the None object is a
> singleton, so anything that *equals* None *is* None.
Twice in one day. Have they put funny chemicals in the water over there?
*wink*
Steve, in case you missed my earlier response:
>>> class Empty:
... def __eq__(self, other):
... return not bool(other)
...
>>> e = Empty()
>>> e == None
True
>>> e is None
False
An instance that compares equal to anything false doesn't strike me as
particularly bizarre or pathological. For instance, the Python Cookbook
has an implementation for the Null object pattern. The implementation
given compares unequal to everything, but suggests defining an
appropriate __eq__ if you need different behaviour.
--
Steven
More information about the Python-list
mailing list