Default scope of variables

Steven D'Aprano steve+comp.lang.python at
Thu Jul 4 07:45:11 CEST 2013

On Thu, 04 Jul 2013 05:30:03 +0100, Joshua Landau wrote:

> That said, I'm not too convinced. Personally, the proper way to do what
> you are talking about is creating a new closure. Like:
> for i in range(100):
>     with new_scope():
>         for i in range(100):
>             func(i)
>     func(i) # Using i from original loop
> But it's not like Python'll ever support that.

Probably not, but Python does support this:

for i in range(100):
    for j in range(100):
    func(i) # Using i from original loop

which solves the problem of inner i overwriting outer i nicely.


More information about the Python-list mailing list