Re: is None or == None ?

View: New views
3 Messages — Rating Filter:   Alert me  

Parent Message unknown Re: is None or == None ?

by Stefan Behnel-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

mk, 06.11.2009 14:20:
> Some claim that one should test for None using:
>
> if x is None:

Which is the correct and safe way of doing it.


> ..but the standard equality which is theoretically safer works as well:
>
> if x == None:

Absolutely not safe, think of

   class Test(object):
       def __eq__(self, other):
           return other == None

   print Test() == None, Test() is None

Stefan
--
http://mail.python.org/mailman/listinfo/python-list

Re: is None or == None ?

by mk-32 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stefan Behnel wrote:
> mk, 06.11.2009 14:20:
>> Some claim that one should test for None using:
>>
>> if x is None:
>
> Which is the correct and safe way of doing it.

ok

>> ..but the standard equality which is theoretically safer works as well:
>>
>> if x == None:
>
> Absolutely not safe, think of
>
>    class Test(object):
>        def __eq__(self, other):
>            return other == None
>
>    print Test() == None, Test() is None

Err, I don't want to sound daft, but what is wrong in this example? It
should work as expected:

 >>> class Test(object):
...     def __eq__(self, other):
...         return other == None
...
 >>> Test() is None
False
 >>> Test() == None
True

My interpretation of 1st call is that it is correct: instance Test() is
not None (in terms of identity), but it happens to have value equal to
None (2nd call).

Or perhaps your example was supposed to show that I should test for
identity with None, not for value with None?

That, however, opens a can of worms, sort of: whether one should compare
Test() for identity with None or for value with None depends on what
programmer meant at the moment.

Regards,
mk





--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown Re: is None or == None ?

by Stefan Behnel-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

mk, 06.11.2009 15:32:

> Stefan Behnel wrote:
>>    class Test(object):
>>        def __eq__(self, other):
>>            return other == None
>>
>>    print Test() == None, Test() is None
>
> Err, I don't want to sound daft, but what is wrong in this example? It
> should work as expected:
>
> >>> class Test(object):
> ...     def __eq__(self, other):
> ...         return other == None
> ...
> >>> Test() is None
> False
> >>> Test() == None
> True

Yes, and it shows you that things can compare equal to None without being None.


> Or perhaps your example was supposed to show that I should test for
> identity with None, not for value with None?

Instead of "value" you mean "equality" here, I suppose. While there are
certain rare use cases where evaluating non-None objects as equal to None
makes sense, in normal use, you almost always want to know if a value is
exactly None, not just something that happens to return True when
calculating its equality to None, be it because of a programmer's concious
consideration or buggy implementation.

Stefan
--
http://mail.python.org/mailman/listinfo/python-list