Built-in Functions: A Reference Guide
Treat built-in function names as reserved names in your own code, even though Python technically allows you to use them for variables.
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?
- 1Initial meaning
Before the assignment, max refers to the built-in function.
- 2Assignment
The variable assignment gives max a new meaning in that scope and shadows the built-in function.
- 3Later use
A later call using max reaches the variable rather than the original built-in function.
- 4Debugging 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
Common mistake Using max as a variable name because it seems short and convenient.
What it looks likeA program stores a value in max and later expects max to behave as the built-in function.
Why it failsThe variable shadows the built-in function in that scope, so a later call using max cannot reach the original built-in through that name.
Do this insteadUse a descriptive name such as maximum_value or max_score.
Common mistake Assuming the assignment is the only place where the problem will appear.
What it looks likeThe assignment completes, but a later part of the program attempts to call the same name.
Why it failsShadowing often creates a bug at the later call site rather than at the assignment site.
Do this insteadWhen diagnosing a failed built-in call, inspect earlier variable assignments for the same name.
Common mistake Ignoring built-in names other than max.
What it looks likeA variable is named len, sum, print, min, or type.
Why it failsThese names also refer to commonly used built-in functions and can be hidden by variables.
Do this insteadTreat 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
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
- 1Built-in functions such as print, len, max, min, sum, and type are available without importing anything.
- 2Treat built-in function names as reserved names when choosing variables.
- 3A variable with the same name shadows the built-in function in that scope.
- 4The resulting error or confusion may appear when the name is called later, not where it was assigned.
- 5Descriptive names such as
maximum_value,max_score,item_count, andtotal_scorepreserve 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.