Sub topic 11
Built-in Names and Accidental Shadowing
This chapter assumes you know how to define and call functions, how the LEGB lookup rule orders local, enclosing, global and built-in scopes, and how to read a simple traceback.
The built-in scope
The last scope Python checks is the built-in scope. It holds names that exist in every program without an import, such as print, len, list, str, sum, max and input. Python looks there only after the local, enclosing and global scopes have all failed to supply the name.
That ordering has a consequence. If you bind a name in an earlier scope, that binding hides the built-in of the same name. This is called shadowing. The built-in is not deleted or changed. It is just never reached, because the search stops at the first match.
A shadowing bug
Naming a variable list looks harmless because the name reads well. It breaks later code that expects the built-in.
list = [1, 2, 3]
print(list("abc"))
# TypeError: 'list' object is not callableThe assignment at module level creates a global name list. When list("abc") runs, lookup finds your list in the global scope and tries to call it. A list object is not callable, so Python raises TypeError.
The error appears on a later line than the mistake. This is why shadowing is hard to debug. The line that fails is correct, and the line that caused the failure may be far away.
Shadowing inside a function
Shadowing also happens locally. Here the damage stays inside one function:
def total(values):
sum = 0
for v in values:
sum += v
return sum
print(total([1, 2, 3]))
print(sum([1, 2, 3]))
# Output:
# 6
# 6Inside total, sum is a local name, so the built-in is hidden only there. The function works, but you could not call sum(values) in that body. Outside the function, the built-in is intact.
Recovering the built-in
If you shadow a name at module level, you can remove your binding with del. Lookup then falls through to the built-in scope again.
list = [1, 2, 3]
del list
print(list("abc"))
# Output: ['a', 'b', 'c']You can also reach the original through the builtins module, which holds the built-in scope:
import builtins
list = [1, 2, 3]
print(builtins.list("abc"))
# Output: ['a', 'b', 'c']These are repairs, not habits. The better fix is to rename your variable.
Finding and avoiding shadows
You can check whether a name is already taken by looking at the built-in module:
import builtins
for name in ["list", "id", "max", "items"]:
print(name, hasattr(builtins, name))
# Output:
# list True
# id True
# max True
# items FalseCommon accidental shadows are list, dict, str, id, type, input, max, min, sum, file (in old code), filter and map. Habits that help:
- Use descriptive names such as
numbers,user_idormax_valueinstead of the bare built-in name. - Add a trailing underscore when the short name is natural, as in
type_orid_. - Turn on a linter rule for shadowed built-ins, such as flake8-builtins (
A001,A002) or Ruff'sArules.
Function parameters count too. def parse(input): shadows input inside parse, and most linters flag it.
Exercise
A script starts with max = 10. Later, in the same module, a function calls max(3, 7) and raises TypeError: 'int' object is not callable. Explain why, and give two ways to fix it.
Show answer
The assignment max = 10 creates a global name max. When the function looks up max, the global scope matches before the built-in scope is reached, so Python tries to call the integer 10. Fixes: rename the variable (for example max_value = 10), which keeps the built-in available; or remove the binding with del max before the call, or call builtins.max(3, 7) after import builtins. Renaming is the preferred fix.