[Python-Dev] bool(container) [was bool(iter([])) changedbetween 2.3 and 2.4] (original) (raw)

Michael Chermside mcherm at mcherm.com
Fri Sep 30 18:23:10 CEST 2005


Jim Jewett writes:

Python doesn't worry about a precise boolean object, it distinguishes between something and nothing.

Is there anything left?" is a pretty good analogy for iterators. [...] A Queue.Queue is always true. Should I submit a bug patch

I would have phrased this very differently. I would have said it as follows:

All Python objects can be converted to a boolean value. Most objects are True, except for False (of course), None, any number equal to 0, an empty string (""), and the empty containers (), [], and {}. User defined objects can override len or nonzero to return False; everything else is True.

I don't believe that there is an overarching rule that all "containers" must be false when empty. I'm not sure there is even a clear definition of "container" that would satisfy everyone. It makes complete sense to me that empty iterators and Queue.Queues are True... it follows the general rule that everything is true except the above (short) list of objects and user-defined (or library) classes that want to mimic the behavior of one of these or have some obvious meaning for non-true.

-- Michael Chermside



More information about the Python-Dev mailing list