Concepts / Scope and Variable Lifetime in Functions

Scope and Variable Lifetime in Functions

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.

  • Programming

The Same Name, Different Variable

A variable name can appear inside a function and elsewhere in a program without referring to one shared variable. When a variable is declared inside a function definition, its name is local to that function. An outside variable with the same name is not related to it. Scope describes the part of the program in which a variable name belongs.

valueoutside the functionvalueinside the function
How can a variable inside a function be different from an outside variable with the same name?

Reading a Function's Local Scope

A function definition creates a block in which names declared inside that function are local. The scope of such a name is the function's block. This means that the name belongs to that function's local context rather than being related to an outside name with the same spelling.

containsdeclaresProgramFunction blocklocal namedeclared inside function
Which parts of the program can access a variable declared inside a function?

Two Independent Names

Consider an outside variable named total and a variable named total declared inside a function. Are they the same variable?

Locate the first name: The first total is outside the function, so it belongs to the outer part of the program.

Locate the second name: The second total is declared inside the function, so its name is local to that function.

Compare their relationship: The source states that variables declared inside a function are not related to outside variables with the same names.

The two total names refer to separate scope contexts rather than one shared variable.

Scope Starts at Definition

A variable has the scope of the block in which its name is declared, beginning at the point where that name is defined.

The phrase beginning at the point of definition matters. It does not describe the entire block without qualification. Instead, the name's scope begins when the name is defined and continues within the relevant block according to the language rules governing that block.

reachescontinuesBefore definitionoutside the name's statedscopeDefinition pointscope beginsAfter definitionwithin the block
From which point in a block can a newly declared variable be used?

Tracing Local Lifetime Carefully

Scope and lifetime are related questions, but they are not identical. Scope asks where a name belongs in the program. The provided source establishes that a name declared inside a function has the function's block as its scope and that the scope begins at the point of definition. It does not specify a separate rule describing exactly when the variable is created or removed during function execution.

containsstarts scope inneeds more rules forFunction definitionName definitionscope begins hereFunction blockstated scopeAdditional lifetimerulenot specified in the source
How should the scope of a function-local variable be traced without assuming unspecified lifetime rules?

Changing an Outside Name

The source identifies a special rule for assigning a value to a name defined at the top level of the program. If the assignment occurs inside a function, Python requires the global statement to tell Python that the name is not local. Without that statement, the source says it is impossible to assign to a variable defined outside the function.

Recognizing the Global Case

A programmer wants an assignment inside a function to affect a name defined at the top level. Which issue must be addressed?

Identify the name's original location: The name was defined at the top level, outside the function.

Identify the assignment location: The assignment is being made inside a function, where names are local unless Python is told otherwise.

Apply the stated rule: The source says to use the global statement when assigning to a top-level name from inside a function.

The global statement is required for the assignment to target the top-level name rather than being treated as a local assignment.

Mistakes Beginners Make

  • Assuming that identical spellings identify one shared variable.

    The source states that a variable declared inside a function is not related to an outside variable with the same name.

    Fix: Classify each name by the block in which it is declared.

  • Treating a function's local name as belonging to the entire program.

    The name has the scope of the function's block.

    Fix: Start by locating the function block, then locate the name's definition inside that block.

  • Ignoring the point of definition.

    The source says that scope starts at the point of definition.

    Fix: Mark the definition point before deciding where the name's scope begins.

  • Expecting an assignment inside a function to change a top-level name automatically.

    The source says that the global statement is required to assign to a top-level name from inside a function.

    Fix: Use the global statement when the intended target is a top-level name.

Scope Tracing Practice

EASY

A program contains an outside name called message and a second name called message declared inside a function. Explain whether the two names are related, identify the scope of the inner name, and state where that scope begins.

Hints
  • Compare the blocks in which the two names are declared.
  • Use the source definition of a function-local name.
  • Remember that scope begins at the point of definition.
MEDIUM

A function attempts to assign a value to a name defined at the top level. What declaration does the source say is needed, and what does that declaration communicate?

Hints
  • Look for the source rule about assigning to a top-level name.
  • The required declaration tells Python that the name is not local.

Key Takeaways

  1. A name declared inside a function is local to that function's block.
  2. An inside name and an outside name with the same spelling are not related according to the source.
  3. A variable's scope begins at the point where its name is defined.
  4. Assigning to a top-level name from inside a function requires the global statement described by the source.
  5. The source establishes scope rules but does not provide a complete rule for the variable's creation and removal over time.

Key Takeaways

  • Function-local names belong to the function block in which they are declared.
  • The same spelling in an outside scope does not make two variables related.
  • Scope begins at the point of definition.
  • The global statement is required when assigning inside a function to a top-level name.
  • Scope and lifetime should not be treated as identical concepts without additional language rules.