Truth value of iterators [was: question about True values)

Robert Kern robert.kern at gmail.com
Thu Oct 26 23:13:28 EDT 2006


Ben Finney wrote:

> Would it make sense to *define* a truth value for iterators? Or at
> least to enable those that *are* able to say "I'm empty" to do so in a
> way that boolean contexts can interpret as "false"?
> 
> Perhaps allowing (but not requiring) an iterator object to grow a
> 'len' method is the simplest way.

And indeed, they are already allowed to do so.

Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
Type "copyright", "credits" or "license" for more information.

In [1]: len(iter(()))
Out[1]: 0

In [2]: bool(iter(()))
Out[2]: False

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list