what exactly is "None" ?

Mark McEahern marklists at mceahern.com
Tue Mar 4 11:28:56 EST 2003


> I know that "None" is the Boolean false value that
> gets thrown out when there's nothing to return or a
> function doesn't manage to reach the end of itself and thus "return
> whatever", but *what* (in Python terms) exactly is equal to "None" ???
>
> I ask this as I'm trying to set up some conditional statements that do
> something if "whatever" is equal to "None".

None is a singleton.  I don't know whether it's correct to say that it's a
Boolean false value.

Anyway, the way to compare to it is like this:

  if x is None:

This also works:

  x = None
  if not x:
    print "See, None evaluates as false--perhaps that's what you meant?"

The reason to prefer the former over the latter depends entirely on whether
you want other things, like [], {}, 0, instances of classes that define
__len__, etc. to also potentially be considered in that branch.  If you
*only* want None to pass the test, the way to do that is with the identity
comparison:

  if x is None:

Cheers,

// m


-






More information about the Python-list mailing list