[Python-3000] Iterators for dict keys, values, and items == annoying :)
Ian Bicking
ianb at colorstudy.com
Thu Mar 23 23:29:15 CET 2006
Guido van Rossum wrote:
> 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'm testing if it is empty or not, which seems natural enough. Or would
be, if it worked. So I start out doing:
for item in select_results: ...
Then I realize that the zero-item case is special (which is common), and do:
select_results = list(select_results)
if select_results:
...
else:
for item in select_results:...
That's not a very comfortable code transformation. When I was just
first learning Python I thought this would work:
for item in select_results:
...
else:
... stuff when there are no items ...
But it doesn't work like that.
.iterkeys() does return an iterator with a useful __len__ method, so the
principle that iterators shouldn't be tested for truth doesn't seem right.
(Very small mostly unrelated problem that occurs to me just at this
moment -- I can't override __len__ with any implementation that isn't
really cheap, because lots of code calls __len__ under the covers, like
list() -- originally SQLObject used len(query) to do a COUNT(*) query,
but that didn't work)
>>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.
Yes, I would expect them to have a good __repr__, and so it wouldn't be
a problem.
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-3000
mailing list