This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 6: Function scope

Local scope across functions

face Josiah Wang

This local scope also applies from inside a function.

Try running the code below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def negate(x):
    return -x

def absolute_value(number):
    threshold = 0
    if number < threshold:
        # this should give an error
        print(x) 
        return negate(number)
    return number

result = absolute_value(-5)

Again, you should get a similar error. x can only be accessed in Lines 1 and 2.