From: Yuh-Ruey Chen Date: 2008-11-01T03:49:16+09:00 Subject: Re: How to access to local variables in enclosing scopes? On Oct 31, 1:31 pm, Pit Capitain wrote: > 2008/10/31 Yuh-Ruey Chen : > > > x = 10 > > > def foo > >        # how to access x from here? > > end > > > class Klass > >        # how to access x from here? > >        def bar > >                # how to access x from here? > >        end > > end > > > And no, I don't want to have to use global variables - that would just > > be pollution and would be incredibly unwieldy in large projects. > > What is the difference between your "local" variable x, which should > be accessible from everywhere, and a global variable? > > Regards, > Pit The difference that the local variable doesn't have to be defined in global scope. Only child scopes should have access to that local variable. def foo a = 10 def bar # should somehow be able to access a end bar end foo # here, we should not be able to access foo's a, but if there is another a in scope, we can access that Or to put it in Python: a = 20 def foo(): a = 10 def bar(): print a # prints 10 bar() foo() print a # prints 20