Loop-and-a-half (Re: Curious assignment behaviour)
David Glasser
news at davidglasser.net
Sat Oct 13 16:47:52 EDT 2001
Paul Rubin <phr-n2001d at nightsong.com> wrote:
> Just today I had a bug that went something like:
>
> n = compute_number_of_items()
> for i in range(n):
> try: process_an_item(items[i])
> except: break
> update_database('number of items processed = ' % i)
[but for n = 0, i never gets defined]
I'm confused. Won't this give the wrong answer, anyway? If you do
process all n correctly, i will be set to n-1. So you'll have to log
"i+1". And by this point you might as well just increment a variable
every time you process an item correctly, which is a lot clearer than
using the value of a loop index outside of the loop, in my opinion.
--
David Glasser
news at davidglasser.net http://www.davidglasser.net/
More information about the Python-list
mailing list