0

I wrote something like this where I am able to access a variable that is used through pythons in for loop syntax and then being able to access that variable, I was wondering if there is a specific reason to being able to do this

for i in array:
    do_something(i)

b = i
Slymodi
  • 185
  • 3
  • 13
  • Because they're not out of score. It's in the scope of the containing function (if there is one), or global (if not inside a function). – Tom Karzes Jun 06 '19 at 16:30

1 Answers1

2

From Short description of the scoping rules?: "The for loop does not have its own namespace"

This means that variables declared in the loop are available for the rest of the code in the same scope as the loop itself

ted
  • 13,596
  • 9
  • 65
  • 107