[Python-3000] Iterators for dict keys, values, and items == annoying :)
Ian Bicking
ianb at colorstudy.com
Fri Mar 24 02:21:24 CET 2006
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. 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.
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-3000
mailing list