User User - 5 months ago 6
Python Question

Why does the scope work like this?

This question is a result of a student of mine asking a question about the following code, and I'm honestly completely stumped. Any help would be appreciated.

When I run this code:

#test 2

a = 1

def func2(x):
x = x + a
return(x)

print(func2(3))


it works perfectly fine. It is able to take the globally-scoped variable a and use its value to perform the calculation and return the value 4.

However, if I change it to this:

# test 3

a = 1

def func3(x):
a = x + a
return(x)

print(func3(3))


I then get an error:


local variable 'a' referenced before assignment


Why do I get this error only when I want to update the value of a within the function to a new value based on its original value? What am I not understanding? I feel like this second piece of code should work fine.

Thanks in advance for any help and insight.

Answer
a = 1

def func3(x):
    global a
    a = x + a
    return(x)

print(func3(3))

Now it should work.

When you put the statement a=x+a inside the function, it creates a new local variable a and tries to reference its value(which clearly hasnt been defined before). Thus you have to use global a before altering the value of a global variable so that it knows which value to refer to.

EDIT:

The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.