while true: !!!

Tim Hochberg tim.hochberg at ieee.org
Tue Dec 19 10:54:08 EST 2000


"David Porter" <jcm at bigskytel.com> wrote in message
news:mailman.977231769.15324.python-list at python.org...
> * Alex Martelli <aleaxit at yahoo.com>:
> > "Rainer Deyke" <root at rainerdeyke.com> wrote in message
> > news:KTz%5.48644$x6.22165257 at news2.rdc2.tx.home.com...
> > >
> > > 0 is false, None is false, [] is false, () is false, {} is false, and
x is
> > > false where x is a class instance and x.__nonzero__() returns false.
> > > Everything else is true.  I think.
> >
> > Another couple of 'false's: a class instance which doesn't define
> > __nonzero__ but does define __length__ when the latter returns 0;
> > and, C-implemented user objects, similarly to class instances.
> >
> > I don't _think_ there are any more...
>
> An empty string is also false.

also 0.0, 0L, 0+0j =>  anything the equals zero.

This can be simplified somewhat by replacing [], (), {}, and "a class
instance which doesn't define __nonzero__ but does define length when the
later returns 0" with "a sequence of length zero".


If I attempt to summarize I get the following for false items:

* None
* Anything that equals zero (Including a class instance with __nonzero__
returning false)
* An empty sequence (Including a class instance with __len__ returning 0)

Coincendentally, this corresponds with the definition in section 5.10 of the
Python Reference Manual:

"In the context of Boolean operations, and also when expressions are used by
control flow statements, the following values are interpreted as false:
None, numeric zero of all types, empty sequences (strings, tuples and
lists), and empty mappings (dictionaries). All other values are interpreted
as true."

-tim






More information about the Python-list mailing list