comparison with None

Paul McGuire ptmcg at austin.rr.com
Wed Apr 18 18:34:40 EDT 2007


On Apr 18, 5:19 pm, Steven Howe <howe.ste... at gmail.com> wrote:
> Alan G Isaac wrote:
> >  >>> None >= 0
> > False
> >  >>> None <= 0
> > True
>
> > Explanation appreciated.
>
> > Thanks,
> > Alan Isaac
>
> I've read and found that 'None' comparisons is not always a good idea.
> Better to:
> from types import NoneType
>
> x = None
> if type( x ) == NoneType:
>     # true
>     < code >
> else:
>     # false; do something else.
>     < more code >
>
> Steven Howe

None is a singleton - there is but one None and no other.  The only
comparisons that make sense with None are "is" or "is not".  type(x)
== NoneType is unnecessary, x is None is sufficient.

>>> x = None
>>> x is None
True
>>> y = None
>>> x is y
True
>>> z = object()
>>> z is None
False
>>> z is not None
True
>>> x is not None
False
>>> y is not None
False

-- Paul





More information about the Python-list mailing list