Quarry School

Trace a local name

Subject: Computing
Level 1 · FoundationsAbout 8 min read2 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    A workshop lends each visitor a tray of tools at the door.

    Rule

    A parameter, and any name assigned anywhere in a function's body, is local to one call of that function. A name that is neither a parameter nor assigned in the body is looked up among the global names, and after those among Python's built-in names. An assignment in the body makes or changes a local name and leaves a global name of the same spelling as it was. Outside the function the local names do not exist.

    Step

    1. Draw two columns for names: one for the file's global names and one for the call's local names.

    2. At a call, start a fresh local column and write each parameter in it with the argument it received.

    3. When the body uses a parameter or a name that it assigns, read it from the local column. If that local name has no value yet on that line, the program stops with an UnboundLocalError. For any other name, read the global column, and after that Python's built-in names such as print.

    4. At an assignment inside the body, first work out the right side, reading each name as the step before says. Then write or change the name on the left in the local column. Leave the global column alone.

    5. At a return line, work out the value to give back while the local column is still there.

    6. When the call ends, cross out the whole local column. If the rest of the program needs the returned value, give it a name at the call.

    Trap

    The common surprise is expecting an assignment inside a function to change a global name. In the counter program the global count still holds 5 after reset() has run. To change a value for the rest of the program, have the function return the new value and assign it at the call. With a reset whose body ends in return 0, the line count = reset() gives the global count the value 0.

    A sharper form of the same slip is a line such as count = count + 1 inside a function, with count assigned only at the margin. The assignment makes count local, so the right side asks for a local name that has no value yet, and the program stops with an UnboundLocalError, which is a kind of NameError. Pass the value in as an argument and return the result.

    1 In plain words

    A workshop lends each visitor a tray of tools at the door. The visitor uses the tools inside and hands the tray back on the way out. The next visitor gets a fresh tray, and nothing on a tray leaves the workshop.

    A function works with names in the same way. A name that gets its value inside a function, including each parameter, is a local name. Each call gets its own set of local names, and they are gone when the call ends.

    A name that gets its value in the file's own lines, outside every function, is a global name. A function can read a global name. An assignment inside a function makes a local name, and that is true when a global name of the same spelling exists as well.

    Remember one sentence: a name assigned inside a function lives and ends with one call. To look it up, see section 4.2 of the Python 3.13 language reference, on naming and binding.

    2 Picture it

    NameInside resetAfter the callcount (local)0gonecount (global)not changed5

    A table of two names that are both spelled count. The local count holds 0 inside the function reset and is gone after the call. The global count is not changed inside reset and holds 5 after the call. The row for the global name is marked.

    Two names spelled count in the counter program

    3 Why

    Python decides which names are local to a function from its definition, before any call runs: the parameters named in the def line and every name the body assigns. At the start of each call the parameters get their values, and each other local name has no value until its assignment line runs. When the body uses a parameter or a name that it assigns anywhere in the body, even on a line above the assignment, Python uses the local name. For any other name it looks among the global names, and after those among Python's built-in names, which is where print is found.

    That order explains the counter program. Inside reset, the line count = 0 makes a local count. The print line in the body uses that local name and displays 0. The global count was never touched, so the last line of the file displays 5.

    When the call ends, its local names end with it. A line outside every function cannot reach them: Python looks for the name among the global names and then the built-in names, and if neither has that spelling, the program stops with a NameError. Another function that has the same spelling as a parameter, or assigns it, gets a local name of its own. One that does neither looks among the global and built-in names, and stops with a NameError when neither has it. Either way it never reaches this call's local name.

    4 The rule

    A parameter, and any name assigned anywhere in a function's body, is local to one call of that function.

    A name that is neither a parameter nor assigned in the body is looked up among the global names, and after those among Python's built-in names. An assignment in the body makes or changes a local name and leaves a global name of the same spelling as it was. Outside the function the local names do not exist.

    5 How

    1. Draw two columns for names: one for the file's global names and one for the call's local names.
    2. At a call, start a fresh local column and write each parameter in it with the argument it received.
    3. When the body uses a parameter or a name that it assigns, read it from the local column. If that local name has no value yet on that line, the program stops with an UnboundLocalError. For any other name, read the global column, and after that Python's built-in names such as print.
    4. At an assignment inside the body, first work out the right side, reading each name as the step before says. Then write or change the name on the left in the local column. Leave the global column alone.
    5. At a return line, work out the value to give back while the local column is still there.
    6. When the call ends, cross out the whole local column. If the rest of the program needs the returned value, give it a name at the call.

    6 Worked example

    Assign a name that also exists outside

    Python code
    count = 5
    
    def reset():
        count = 0
        print(count)
    
    reset()
    print(count)
    

    Standard input

    Empty

    Standard output

    0
    5
    

    Predict the two output lines first. Then save the code as counter.py and run the file, or follow the trace below. The assignment in the body makes a local name, and the global name keeps its value.

    Use a local name outside its function

    Python code
    def add_tip(bill):
        tip = 2
        return bill + tip
    
    print(add_tip(10))
    print(tip)
    

    Standard input

    Empty

    Standard output

    12
    

    Exception NameError

    Predict what happens on each of the last two lines first. Then save the code as tip.py and run the file. The call returns 12, which is displayed. The last line asks for tip outside the function, where no such name exists, and the program stops with a NameError.

    Read a global name inside a function

    Python code
    rate = 3
    
    def cost(hours):
        return hours * rate
    
    print(cost(4))
    

    Standard input

    Empty

    Standard output

    12
    

    Predict the output line first. Then save the code as rate.py and run the file. The body assigns nothing to rate, so Python finds no local name of that spelling and uses the global one.

    Trace

    Trace the counter program
    InstructionGlobal countLocal countOutputExplanation
    count = 55none—An assignment at the margin makes a global name.
    def reset():5none—The def line stores the body. No local name exists yet.
    reset()5no value yet—The call starts. count is local to reset, because the body assigns it, but it has no value until that line runs.
    count = 050—The assignment in the body makes a local count. The global count stays 5.
    print(count)500The body assigns count, so this line uses the local count.
    print(count)5none5The call has ended and its local name is gone. This line is at the margin, so it uses the global count.

    7 The trap

    The common surprise is expecting an assignment inside a function to change a global name. In the counter program the global count still holds 5 after reset() has run. To change a value for the rest of the program, have the function return the new value and assign it at the call. With a reset whose body ends in return 0, the line count = reset() gives the global count the value 0.

    A sharper form of the same slip is a line such as count = count + 1 inside a function, with count assigned only at the margin. The assignment makes count local, so the right side asks for a local name that has no value yet, and the program stops with an UnboundLocalError, which is a kind of NameError. Pass the value in as an argument and return the result.

    Quick checks

    Quick check 1 of 2

    Predict the two lines this program displays. The program reads no input.

    Python code
    color = "white"
    
    def paint():
        color = "green"
        print(color)
    
    paint()
    print(color)
    Show the steps and the answer
    Answer

    It displays green and then white.

    Why: The assignment in the body makes a local color, and the print line in the body uses it. The global color is not changed, and the last line displays it.

    Source: Python Software Foundation, Python 3.13 Tutorial, 4.8 Defining Functions

    Quick check 2 of 2

    This program stops with an error on its last line. Which name causes it, and what one change to the last two lines displays 9?

    Python code
    def add(a, b):
        total = a + b
        return total
    
    add(4, 5)
    print(total)
    Show the steps and the answer
    Answer

    The name total causes it, because total is local to add. Give the returned value a name at the call and print that name: result = add(4, 5) and then print(result).

    Why: total exists only during the call. The call returns 9, and a name assigned at the margin can hold that value for the last line.

    Source: Python Software Foundation, Python 3.13 Language Reference, 4.2 Naming and binding

    Sources

    1. Python 3.13 Tutorial, 4.8 Defining Functions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    2. Python 3.13 Language Reference, 4.2 Naming and binding. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.