[Python-3000] Iterators for dict keys, values, and items == annoying :)

Guido van Rossum guido at python.org
Fri Mar 24 02:31:27 CET 2006


On 3/23/06, Ian Bicking <ianb at colorstudy.com> wrote:
> Guido van Rossum wrote:
> > But this is only needed if *all you have* is the iterator. Most of the
> > time, the code containing the for loop has access to the container,
> > and the iterator is only instantiated by the __iter__() call implied
> > by the for loop.
>
> I don't think that is the case.  For instance:
>
> def non_empty_lines(seq):
>      for line in seq:
>          if line.strip() and not line.strip().startswith('#'):
>              yield line
>
> for line in non_empty_lines(open('config.txt')):
>      ...
>
> I think wrapping the iterator in non_empty_lines() shouldn't cause you
> to have to rewrite your logic to radically.

Radically compared to what?

> More generally, I find
> myself using list() fairly often lately as generators have become more
> popular, and it's not just with SQLObject.  Testing for the existence of
> any items in the iterator (is that a better way of saying it than
> empty?) is often the reason.

If creating a copy of all items using list() is not a problem, then
you shouldn't have been using iterators in the first place. Iterators
exist so you can efficiently handle cases where list() would overflow
memory. If you don't have such cases, you should just design your APIs
to return lists in the first place.

But I betcha that many of the APIs you're using are giving you
iterators instead of lists *because* (in the general case -- maybe not
for your application) they can return more data than fits in memory.
SQL queries being an example.

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list