variable scoping problem

Alexander Schmolck a.schmolck at gmx.net
Thu Apr 24 21:15:57 EDT 2003


Tung Wai Yip <tungwaiyip at yahoo.com> writes:

> Consider this
> 
> >>> x=1
> >>> def foo():
> ...   print x
> ...
> >>> def bar():
> ...   #print x   <- referenced before assignment error
> ...   x=2
> ...   print x
> ...
> >>> foo()
> 1
> >>> bar()
> 2
> >>> print x
> 1
> >>>
> 
> What rules in Python explain the scoping of x? Is there a name for the
> scope of the first x? 'module' scope? Why is bar() not able to access
> x but foo() can?

Q1: global scope

Q2: Because when python sees that x is assigned to in a function it is treated
as a local variable *anywhere* in that function (even *before* the
assignment); *unless* you've declared it as global in that function. As long
as you don't assign to it, you're fine.

This makes sense -- otherwise you could easily introduce devious bugs by
changing global variables without any intent of doing so. OTOH, you don't want
to have to declare a module, function or a constant global just so that you
can use it from within the function.

Consider (all untested):

def aTest():
    print "HI"

def bTest():
    def aTest(): print "Just a local definition"
    aTest()

then consider:

>>> aTest
HI
>>> bTest()
Just a local definition
>>> aTest()
HI

def aTest():
    print "HI"

def bTest():
    aTest() 
    def aTest(): print "NEVER"
    return aTest(3)
>>> bTest()


and finally:

def aTest():
    print "HI"
def bTest():
    global aTest
    aTest()
    def aTest(): print "REDEFINED GLOBALLY"
    aTest()
>>> aTest()
Hi
>>> bTest()
HI
REDEFINED GLOBALLY
>>> aTest()
REDEFINED GLOBALLY


'as




More information about the Python-list mailing list