def foo(): for i in [1,2,3,4,5]: x = i print(x,i) 

Why the function call does not produce errors, because x and i are defined inside the loop, and they are accessed outside it?

    1 answer 1

    No, the for block does not create a new scope. Both the loop variable and the variables declared in the block will become variable in the enclosing region.

    • That is, you can not use the same variables in two adjacent cycles? - user329125 pm
    • one
      @ user329125, what forbids you to do this? Nothing prevents you from using the same variable name several times. Just the old value at the next assignment will be lost. - insolor