Built-in Functions: A Reference Guide
Built-in function names like max, len, sum, and print should be treated as reserved words and never used as variable names.
The Hidden Cost of a Familiar Name
Python gives you many built-in functions without requiring an import. Names such as print, len, max, min, sum, and type support everyday programming. Because Python also allows you to create variables with almost any name, you can accidentally reuse one of these function names. The assignment may look harmless, but it can hide the built-in function from later code in that scope.
Treat built-in function names as reserved names in your own code, even though Python technically allows you to use them for variables.
Before and After Shadowing
In the before state, max refers to the built-in function. After a variable is assigned the name max, that name refers to the variable instead. Later code that tries to call max is no longer reaching the original built-in function. The important timing is that the visible problem appears when the name is called, not necessarily where the variable was assigned.
How Name Resolution Changes
When code uses a name such as sum, a variable with that name can take precedence over the built-in function in the same scope. This is the mechanism called shadowing: the variable does not remove the built-in from Python, but it hides the built-in behind the variable name for that scope. As a result, code that expects sum to be the familiar function may instead work with the variable.
Tracing a Shadowed Name
Suppose a program first uses max as the name of the built-in function and later assigns a variable named max. What should you expect when the program later tries to call max?
Initial meaning: Before the assignment, max refers to the built-in function.
Assignment: The variable assignment gives max a new meaning in that scope and shadows the built-in function.
Later use: A later call using max reaches the variable rather than the original built-in function.
Debugging point: The resulting problem appears at the later call, even though the cause was the earlier assignment.
The built-in max function is inaccessible through the name max in that scope until the situation is corrected or a workaround is used.
Choosing Names That Preserve Meaning
A descriptive name solves two problems at once. A name such as maximum_value explains what the variable stores, and it leaves max available for its built-in purpose. 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.
| Avoid as a variable name | Prefer a descriptive alternative | Reason |
|---|---|---|
| max | maximum_value | Preserves the built-in name and clarifies the stored value |
| len | item_count | Avoids hiding the built-in and communicates purpose |
| sum | total_score | Keeps the built-in available and makes the variable's meaning clearer |
Generated naming examples based on the source's recommendation to use descriptive alternatives.
The safest solution is prevention: when a possible variable name matches a built-in function, choose a descriptive alternative before the name is used.
Mistakes That Cause Shadowing
Using max as a variable name because it seems short and convenient.
The variable shadows the built-in function in that scope, so a later call using max cannot reach the original built-in through that name.
Fix:
Use a descriptive name such as maximum_value or max_score.Assuming the assignment is the only place where the problem will appear.
Shadowing often creates a bug at the later call site rather than at the assignment site.
Fix:
When diagnosing a failed built-in call, inspect earlier variable assignments for the same name.Ignoring built-in names other than max.
These names also refer to commonly used built-in functions and can be hidden by variables.
Fix:
Treat the commonly used built-in names as protected names and choose alternatives that describe the data.
Practice the Naming Decision
A program needs to store the highest score in a variable. Which name is the better choice: max or max_score? Explain what would happen to the built-in max name if the first choice were used.
Hints
- Identify whether either candidate matches a built-in function name.
- Consider whether the name describes the value being stored.
- Think about what a later call using max would refer to after the assignment.
What do you think happens?
A variable is named max, and later the program tries to call max. Which outcome should you predict?
Reveal answer
Answer: The variable shadows the built-in, so the call cannot use the original built-in through max.
Assigning the variable changes what the name refers to in that scope. The visible problem occurs when later code tries to call the shadowed name.
Practical Rules to Remember
- Built-in functions such as print, len, max, min, sum, and type are available without importing anything.
- Treat built-in function names as reserved names when choosing variables.
- A variable with the same name shadows the built-in function in that scope.
- The resulting error or confusion may appear when the name is called later, not where it was assigned.
- Descriptive names such as maximum_value, max_score, item_count, and total_score preserve built-in functions and make intent clearer.
Key Takeaways
- Python provides many built-in functions without requiring imports.
- Using a built-in function name for a variable shadows the built-in in that scope.
- Shadowing often causes a problem at a later call site, which can make debugging harder.
- Descriptive variable names prevent conflicts and communicate what the variable stores.
- Names such as max, len, sum, print, min, and type should be treated as protected names.