Default scope of variables

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jul 7 12:08:59 EDT 2013


On Fri, 05 Jul 2013 13:24:43 +0000, Neil Cerutti wrote:

> for x in range(4):
>    print(x)
> print(x) # Vader NOoooooOOOOOO!!!

That loops do *not* introduce a new scope is a feature, not a bug. It is 
*really* useful to be able to use the value of x after the loop has 
finished. That's a much more common need than being able to have an x 
inside the loop and an x outside the loop.


-- 
Steven



More information about the Python-list mailing list