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?
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?
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.
Source: https://ru.stackoverflow.com/questions/952325/
All Articles