Concepts / Understanding Variable Assignment and Naming Conventions

Understanding Variable Assignment and Naming Conventions

Built-in function names like max, len, sum, and print should be treated as reserved words and never used as variable names.

  • Programming

The Naming Trap

Python provides many built-in functions that are available without importing anything. Names such as print, len, max, min, sum, and type support everyday programming. Python also lets you create variables using the names you choose. That flexibility creates a trap: if a variable uses the same name as a built-in function, the variable shadows the function in that scope.

Treat commonly used built-in function names as reserved names when choosing variables, even though Python technically allows assignments with those names.

Before and After Shadowing

refers torefers tolenbuilt-in functionfunctionavailablelenvariable namevaluebuilt-in hidden
What changes when a variable named len replaces access to the built-in function?

Assignment changes what a name refers to in the relevant scope. Before a conflicting assignment, len can access Python's built-in function. After assigning a variable named len, that name refers to the variable instead, so the built-in function is shadowed, or hidden, in that scope. The important change is the meaning of the name, not merely the value stored in the variable.

What do you think happens?

Suppose a program first assigns a value to the name max and later tries to call max as a function. What is most likely to happen?

  • The built-in function remains available automatically
  • The variable named max hides the built-in function
  • Python renames the variable to avoid a conflict
Reveal answer

Answer: The variable named max hides the built-in function.

Assigning the same name as a built-in shadows that built-in in the relevant scope. The problem becomes visible when the program later tries to call the shadowed function.

When the Function Call Fails

assigns valueshadowsname lookup reaches variablebuilt-in is inaccessiblelenbuilt-in availablelen = valuevariable assignmentlenbuilt-in hiddenfunction calllater attempterrorcall cannot use theshadowed name
How does assigning a value to a built-in name affect a later attempt to call that function?

Shadowing is easy to miss because the assignment itself may not look like a problem. The error appears later, when the program tries to call the name as though it were still the built-in function. This makes the bug harder to debug: the visible failure is separated from the assignment that caused it.

Tracing a Conflicting Name

A programmer wants to store a highest score and chooses the variable name max. Later, the program needs to use max as the built-in function.

Choose the name: The programmer assigns the highest score to a variable named max. Python permits the name, but it now refers to the programmer's variable in that scope.

Continue the program: The original built-in function named max is shadowed. Code that expects max to be the built-in no longer reaches that function.

Attempt the later call: When the program tries to call max, the failure occurs at that later call rather than at the earlier assignment.

Diagnose the cause: The earlier assignment is the cause. The name max was reused for user data, so the built-in function became inaccessible in that scope.

The name max should be replaced with a descriptive alternative such as max_score, preserving access to the built-in function and clarifying what the variable stores.

User Names and Built-in Names

can causepreserves access tomaxbuilt-in function namemax_scoredescriptive variable nameshadowed functionaccess is lost in the scopebuilt-in maxremains available
What is the difference between a variable name that conflicts with a built-in function and one that clearly refers to user data?
Name choiceWhat it communicatesEffect on the built-in
maxA name that also identifies a built-in functionCan shadow the built-in
max_scoreA value representing a maximum scoreDoes not conflict with max
sumA name that also identifies a built-in functionCan shadow the built-in
total_scoreA total related to scoresDoes not conflict with sum

A descriptive name serves two purposes. It explains what the variable stores, and it avoids taking over a name Python already uses for a built-in function. For example, max_score communicates the purpose of a score value without shadowing max. The same principle applies to names such as len, sum, print, min, type, range, str, int, float, list, dict, set, tuple, open, input, and sorted.

Naming Rules That Prevent Collisions

  1. Notice whether a tempting variable name is also a commonly used built-in function name.
  2. Treat names such as max, len, sum, print, min, and type as reserved for built-in access.
  3. Replace a conflicting name with one that describes the stored data, such as max_score instead of max.
  4. Check later uses of the name: a variable should read like data, while a built-in name should remain available for its function.
  5. If a built-in has already been shadowed, locate the earlier assignment because the later call failure is a consequence of that assignment.

The safest habit is simple: do not use built-in function names as variable names. This costs nothing, makes the code's intent clearer, protects access to everyday functions, and prevents bugs whose symptoms appear far from their cause.

  • Using max as a variable name for a highest value

    max is also the name of a built-in function. The assignment shadows that function in the relevant scope.

    Fix: Use a descriptive name such as max_score.

  • Looking only at the line where the error appears

    The assignment that caused the shadowing occurred earlier, so the visible error location can be misleading.

    Fix: Search earlier code for an assignment using the built-in's name.

  • Assuming Python will reject every conflicting name immediately

    Python permits variables to use these names, even though doing so hides the built-in function.

    Fix: Apply the convention yourself and choose a non-conflicting descriptive name.

Check Your Naming Decision

EASY

A program needs variables for a student's highest score, the number of submitted answers, and the total of all scores. Choose names that describe the data without shadowing the built-in names max, len, or sum.

Hints
  • Identify which tempting names are also built-in function names.
  • Add a word that explains what each value represents.
  • Prefer names that communicate the data's purpose.

One Possible Naming Review

Review names for a highest score, a number of submitted answers, and a total score.

Highest score: Use max_score rather than max because max is a built-in function name.

Number of answers: Use answer_count rather than len because len is a built-in function name.

Total score: Use total_score rather than sum because sum is a built-in function name.

The descriptive names preserve access to max, len, and sum while making the stored data easier to understand.

Key Takeaways

  • Built-in function names such as max, len, sum, and print should be treated as reserved names when choosing variables.
  • Assigning a variable with a built-in's name shadows that function in the relevant scope.
  • The failure often appears later, when the program tries to call the shadowed name, which makes the original assignment difficult to diagnose.
  • Descriptive names such as max_score, answer_count, and total_score avoid collisions and clarify what variables store.
  • Preventing a collision is simpler than recovering access to a built-in after it has been shadowed.