Comment 2 for bug 1408774

Revision history for this message
asmeurer (asmeurer) wrote :

I don't know if any code uses this idiom, but a possible use-case of setting the loop variable before the loop (without using it) is if the loop is empty and you want to use the loop variable after the loop like

i = None
for i in list_that_could_be_empty:
    do_stuff()

do_something_with(i)

If you don't set i above the loop and the loop ends up being empty you'll get a NameError.

Beyond that, I don't see a reason why setting a loop variable shouldn't be included in the "unused variable redefined" warnings.