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

Guido van Rossum guido at python.org
Thu Mar 23 23:10:43 CET 2006


On 3/23/06, Ian Bicking <ianb at colorstudy.com> wrote:
> This has been my personal experience with the iterators in SQLObject as
> well.  The fact that an empty iterator is true tends to cause particular
> problems in that case, though I notice iterkeys() acts properly in this
> case; maybe part of the issue is that I'm actually using iterables
> instead of iterators, where I can't actually test the truthfulness.

This sounds like some kind of fundamental confusion -- you should
never be tempted to test an iterator for its truth value.

I wonder if the confusion lies in the API similarities with dict,
which *does* return an iterable (not an iterator) from keys() etc.?
Other objects that return iterators() from the same API are not
obeying the (2.x) API contract for maps.

> Another issue I have with generators (and hence iterators) is the
> uselessness of repr() on them.  This causes a lot of list() invocations
> as well, which works with interactive testing, but fails badly with
> print statements (where you have to do another run with list() around it
> -- but that itself causes problems when exhausting the iterator
> introduces a new bug).

Views could solve this problem as well, since they are reiterable.

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


More information about the Python-3000 mailing list