Understanding Variable Assignment and Naming Conventions
Treat commonly used built-in function names as reserved names when choosing variables, even though Python technically allows assignments with those names.
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
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?
Reveal 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
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.
- 1Choose 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.
- 2Continue 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.
- 3Attempt the later call
When the program tries to call max, the failure occurs at that later call rather than at the earlier assignment.
- 4Diagnose 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
| Name choice | What it communicates | Effect on the built-in |
|---|---|---|
| max | A name that also identifies a built-in function | Can shadow the built-in |
| max_score | A value representing a maximum score | Does not conflict with max |
| sum | A name that also identifies a built-in function | Can shadow the built-in |
| total_score | A total related to scores | Does not conflict with sum |
Descriptive names make a variable's purpose clearer while protecting access to built-in functions.
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
- 1Notice whether a tempting variable name is also a commonly used built-in function name.
- 2Treat names such as max, len, sum, print, min, and type as reserved for built-in access.
- 3Replace a conflicting name with one that describes the stored data, such as
max_scoreinstead of max. - 4Check later uses of the name: a variable should read like data, while a built-in name should remain available for its function.
- 5If 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.
Common mistake Using max as a variable name for a highest value
What it looks likeA variable named max stores a highest score.
Why it failsmax is also the name of a built-in function. The assignment shadows that function in the relevant scope.
Do this insteadUse a descriptive name such as max_score.
Common mistake Looking only at the line where the error appears
What it looks likeThe program fails when it later tries to call a shadowed built-in.
Why it failsThe assignment that caused the shadowing occurred earlier, so the visible error location can be misleading.
Do this insteadSearch earlier code for an assignment using the built-in's name.
Common mistake Assuming Python will reject every conflicting name immediately
What it looks likeA programmer expects an assignment using print or sum to be blocked automatically.
Why it failsPython permits variables to use these names, even though doing so hides the built-in function.
Do this insteadApply the convention yourself and choose a non-conflicting descriptive name.
Check Your Naming Decision
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.
- 1Highest score
Use
max_scorerather than max because max is a built-in function name. - 2Number of answers
Use
answer_countrather than len because len is a built-in function name. - 3Total score
Use
total_scorerather 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, andtotal_scoreavoid collisions and clarify what variables store. - Preventing a collision is simpler than recovering access to a built-in after it has been shadowed.