Default scope of variables

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


On Thu, Jul 4, 2013 at 2:30 PM, Joshua Landau
<joshua.landau.ws at gmail.com> 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):
    print("outer",i)
    def inner():
      for i in range(4):
        print("inner",i)
    inner()
    print("outer",i)

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

ChrisA



More information about the Python-list mailing list