Default scope of variables

Chris Angelico rosuav at
Thu Jul 4 06:36:20 CEST 2013

On Thu, Jul 4, 2013 at 2:30 PM, Joshua Landau
< at> 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.

def foo():
  for i in range(3):
    def inner():
      for i in range(4):

That works, but you then have to declare all your nonlocals, and it
hardly reads well.


More information about the Python-list mailing list