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

Ben Finney ben+python at benfinney.id.au
Sun Jan 17 21:46:42 EST 2016


<paul.hermeneutic at gmail.com> writes:

> I prefer (x is None) and (x is not None).

There are good reasons to prefer this.

But this is not a good reason:

> This matches the SQL concept of NULL.

That's not really helpful, because it *doesn't* match.

> (X = NULL) is not valid since NULL is not a value and cannot be
> compared with anything.

SQL Null fails comparison with any value. Python ``None`` is a value and
can be compared like any other value.

SQL Null is neither truthy nor falsy; three-value logic is required when
Null can occur. Python ``None`` is falsy by definition, and two-value
(Boolean) logic continues to obtain.

SQL Null is not a value and has no data type. Python ``None`` is an
object and, like any other object, has a type defining the operations
that it can perform.

And so on. While some libraries do conflate SQL Null with Python
``None``, the two concepts really behave quite differently. It is
needlessly misleading to say they “match” in any sense.

I'm in the camp that says, while SQL is quite useful, its NULL is a wart
<URL:https://dba.stackexchange.com/questions/5222/why-shouldnt-we-allow-nulls/6049>.

-- 
 \        “… it's best to confuse only one issue at a time.” —Brian W. |
  `\    Kernighan and Dennis M. Ritchie, _The C programming language_, |
_o__)                                                             1988 |
Ben Finney




More information about the Python-list mailing list