Understanding Function Scope
When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function - i.e. variable names are local to the function. This is called the scope of the variable. All variables have the scope of the block they are declared in starting from the point of definition of the name.
Two Names, Two Variables
Imagine that a program has a variable named x outside a function. The function also declares a variable named x. These names look identical, but the two variables are not related. The x declared inside the function belongs to the function's local scope, while the other x belongs to the surrounding scope. Changing one does not make the two variables become the same variable.
Tracing the Function Boundary
Following Two Variables Named score
An outside part of a program has score with the value 50. A function declares its own score with the value 80. What happens to each variable?
Outside declaration: The outside score belongs to the scope of the block where it was declared.
Function declaration: The function's score belongs to the function's local scope. It is not related to the outside score, even though the name is the same.
Separate changes: A change to the function's score concerns the function-local variable. It does not turn that variable into the outside score.
The two score variables are separate because each name is local to its own scope.
A useful way to reason about scope is to attach each variable name to the block in which it is declared. A function definition creates a boundary for names declared inside it. The same spelling can appear outside that boundary without referring to the function's local variable. Scope therefore describes where a declared name belongs, not merely how the name is written.
What the Scope Boundary Contains
The scope of a variable is the block in which its name is declared, beginning at the point where that name is defined. For a variable declared inside a function definition, that block is the function's local scope.
The function boundary prevents an inside declaration from becoming related to an outside declaration with the same name. This is why a function can use a local name without making that name the same variable as one used elsewhere in the program.
When a Name Becomes Available
Scope does not begin at an arbitrary place in the block. The rule is tied to the point where the variable name is defined. Before that point, the name has not yet begun its scope in that block. From the definition point onward, the name has the scope of that block.
Locating the Start of Scope
A block contains several statements, and the name total is defined partway through the block. Identify where total begins to have scope.
Before the definition: The name total has not yet been defined at this point in the block.
At the definition: This is the point at which the name total is introduced.
After the definition: The name total has the scope of the block from this point onward.
The scope of total begins at its point of definition and continues within its declared block.
Assigning to a Top-Level Name
This rule is about assignment to a top-level name from inside a function. It does not erase the distinction between the function's local scope and the top-level scope. Instead, the global statement tells Python that the assignment is intended for the top-level name rather than for a local name.
Mistakes Beginners Make
Assuming that identical names refer to one shared variable.
Variables declared inside a function are not related to variables with the same names outside the function.
Fix:
Track each name together with the block where it was declared.Treating scope as if it starts everywhere in the block at once.
The scope of a variable begins from the point where its name is defined.
Fix:
Mark the definition point first, then treat the name as belonging to its block from that point onward.Assigning to a top-level name inside a function without using global.
The source rule states that assignment to a variable defined outside a function is impossible without the global statement.
Fix:
Use the global statement when the intended assignment is to a top-level name.
Practice the Trace
A program has an outside variable named level. A function declares another variable named level. Write a short explanation that identifies the scope of each name and explains whether the two variables are related. Then state the point at which the function-local level begins to have scope.
Hints
- Name the block where each level is declared.
- Remember that identical names do not make variables related across function boundaries.
- Use the definition point to identify when the local name begins its scope.
Key Takeaways
- A variable's scope is the block where its name is declared.
- A variable declared inside a function is local to that function.
- An inside variable and an outside variable can share a name without being related.
- Scope begins at the point where the variable name is defined.
- The global statement is required when a function assigns to a name defined at the top level.
Key Takeaways
- Function scope keeps names declared inside a function separate from same-named variables outside it.
- The block of declaration determines a variable's scope.
- A variable's scope begins at its definition point.
- Use the global statement when a function must assign to a top-level name.