"x == None" vs "x is None"

Chris Angelico rosuav at gmail.com
Sun Jan 17 16:38:59 EST 2016


On Mon, Jan 18, 2016 at 8:33 AM, Random832 <random832 at fastmail.com> wrote:
> <paul.hermeneutic at gmail.com> writes:
>
>> I prefer (x is None) and (x is not None).
>>
>> This matches the SQL concept of NULL.
>>
>> (X = NULL) is not valid since NULL is not a value and cannot be compared
>> with anything.
>
> The suitably generic SQL operator is "is (not) distinct from", in some
> dialects of SQL [certainly if you're using NULL directly you can simply
> use is/is not, but the "distinct from" operators can be used to compare
> two expressions that may either or both be NULL.
>
> "X is not distinct from Y" == "X = Y or X is NULL and Y is NULL"

It's worth noting that Python's None is not really the same as SQL's
NULL... although that's partly because nothing is. NULL isn't quite
the same as IEEE NaN, nor C's null pointer, nor Python's None, all of
which are quite different from each other. All of them can be used to
represent some of the same concepts, but they behave very differently.
Trying to explain one in terms of another is likely to trip people up
somewhere.

ChrisA



More information about the Python-list mailing list