Jamie Starke bio photo

Jamie Starke

I develop for my career, but I also develop for fun, and whenever I think it can solve a problem I'm having. This has lead me to create side projects, such as Rental Map and BusTimes, among others.

Consulting Newsletter Email LinkedIn Github Stackoverflow More

We covered a number of examples using scope. I have decided to post these examples so that they will hopefully help you all better understand scope.

Scope Example 1: Variables passed through function definitions

def foo(n):
    print n, 'in foo'
    n = 456
    print n, 'in foo'

x = 123
print x, 'in main'
foo(x)
print x, 'in main'

In the example above, when we call foo(x), we pass the function foo(n) a copy of the value of variable x from the main scope. This is now treated as a new variable n within foo(n), so when we change its value, it does not modify the value of x

Scope Example 2: Internal variable has same name as external variable

def foo(n):
    x = 'qwerty'
    print x, 'in foo'
    print n, 'in foo'
    n = 456
    print n, 'in foo'

x = 123
print x, 'in main'
foo(x)
print x, 'in main'

If you look at the example above, you might expect the last print x to display qwerty. This is not the case, as the x variable inside foo and outside foo have different scopes, and are treated independently.

Scope Example 3: Accessing a variable from parent scope

def foo():
    print x, 'in foo'

x = 123
print x, 'in main'
foo()
print x, 'in main'

In this example, the function foo(), when we try to access the variable x, the program recognizes that we never set x from within the scope of foo(), so it will look for x in the parent scope, find that variable x and print its value.

Scope Example 4: Setting a variable from the parent scope

def foo():
    # compile error here, b/c x is seen as a local now
    print x, 'in foo'
    x = 456
    print x, 'in foo'

x = 123
print x, 'in main'
foo()
print x, 'in main'

In the above example, we will get the error

UnboundLocalError: local variable 'x' referenced before assignment

The reason for this is because we are setting the value of the variable x inside foo() the program will try to resolve the value of the variable x within the scope of foo(). As it hasn’t been set yet, the program is unable to do this.

Scope Example 5: Accessing variables in the global scope

def foo():
    global x
    print x, 'in foo'
    x = 456
    print x, 'in foo'

x = 123
print x, 'in main'
foo()
print x, 'in main'

In the above example, the foo() states explicitly that when we refer to the variable x, we mean the variable x from the global scope. So when we first print x in foo() we have the value of 123 from the parent scope. When we set x in foo(), we update the value in the parent scope, so that when we print x at the end, it’s now 456 there as well.

Scope Example 6: Accessing the global scope, continued

def foo():
    global x
    x = 123

foo()
print x

The above example is very similar to Example 5, but we set the variable x from the foo() function before accessing in the global scope.