[Python-Dev] bool(iter([])) changed between 2.3 and 2.4

Terry Reedy tjreedy at udel.edu
Wed Sep 21 01:55:26 CEST 2005

"Guido van Rossum" <guido at python.org> wrote in message 
news:ca471dc20509201449652f11d at mail.gmail.com...
>I just finished debugging some code that broke after upgrading to
> Python 2.4 (from 2.3). Turns out the code was testing list iterators
> for their boolean value (to distinguish them from None).

This seem unnecessarily indirect.
Why not 'it != None' or now, 'it is not None'?

>In 2.3, a
> list iterator (like any iterator) is always true. In 2.4, an exhausted
> list iterator is false; probably by virtue of having a __len__()
> method that returns the number of remaining items.

According to 2.4 News, dict iterators also got __len__ method, though I saw 
no mention of list.

> I realize that this was a deliberate feature,

I presume there were two reasons: internal efficiency of preallocations 
(list(some_it) for example) and letting people differentiate iterator with 
something left to return versus nothing, just as we can differentiate 
collections with something versus nothing.

> and that it exists in
> 2.4 as well as in 2.4.1 and will in 2.4.2; yet, I'm not sure I *like*
> it. Was this breakage (which is not theoretical!) considered at all?

Searching the gmane archive with the link given on the python site for 
'iterator len', I could not find any discussion of the __len__ method 
addition itself.

Terry J. Reedy

More information about the Python-Dev mailing list