Show warning when variable is used multiple times in for loops

Hello everyone,
While using for loops, I notice I easily consider them as local variables that disappear once the loop ends. But it is not the case and a variable declared in a for loop stays after that loop. When using nested loop, it can be easy to use the same variable name for multiple loops and mess up the code.
It would be great if something like a warning is displayed when that happens, so that the programmer knows what’s happening. It seems easy to detect and could easily save time debugging, maybe mostly for beginners but even for more experienced users, I believe.

While I haven’t closely followed Python ideas in the past years, I think this sort of thing has been discussed countless times.

Such a change would be very disruptive for existing code because there are valid cases where reusing the loop variable from a previous loop is really what you want to do.


You shouldn’t, because you are intended to be able to use them after the loop ends and there are many standard idioms that depend on this.

It shouldn’t be. Use more descriptive variable names, and write shorter functions. Consider factoring loop bodies out into separate functions.

This is generally speaking your IDE’s responsibility, not Python’s.

There was a poll where this was discussed at length here that you may want to read through.

However, the results were overwhelmingly in favor of the status quo.

Thank you everyone for your answers.

To be more specific, I don’t think that a variable should disappear when the loop ends, the current behavior seems fine. My point is that sometimes, we can forget it, especially while using the loop to iterate over indices. Mixing up i’s and j’s can happen really quick. I understand though it would be the programmer’s responsibility to find another way of managing that.

It is true it would be IDE’s responsibility to display that behaviour, maybe I posted on the wrong forum.
My point is that it would be nice to be informed if there is multiple affectation for the same variable name, in a nested loop. No need to trigger an error, simply a warning, like for unused imports.

The poll @Axe319 posted is quite different, as explained above.

Thank you for your time :slight_smile:

It looks like ruff may already have a warning for this - see Proposed check: reuse of loop variable in nested loop · Issue #2972 · charliermarsh/ruff · GitHub for a discussion leading up to what looks like it being implemented.

It will not only trigger in your code, but also in the code of all modules that you import, without this being under your control.

By the way, I’m not sure what you mean by “like for unused imports”. Python does not have a warning for unused imports (though most linters do). Like reuse of loop variables, it has its use cases, for example modules whose function is to raise an exception on import if some condition is not met.

Thank you all for your answers. I understand that my idea probably fits more what a linter would do. Probably ruff will do the job fine, thank you very much for the suggestion :slight_smile: