Shadowing occurs when a nearer definition reuses a name and hides an existing binding. In Python, this can make familiar built-ins or earlier functions inaccessible. section 2.3.1 section 2.3.2
Common cases include:
- Defining your own open() hides Python’s built-in open(), even though the replacement may be valid for the immediate program. section 2.3.1
- Defining add() twice in one file causes the second definition to replace the first, regardless of their different behaviors. section 2.3.2
- Placing one implementation in a module preserves both names: local add() and utility.add() refer to separate namespace entries. section 2.3.2
Keep implementations in separate namespaces
A local add() can perform mathematical addition while utility.add() performs string concatenation. Importing utility gives each implementation an explicit qualified name; importing it as utils creates the alias utils.add(). section 2.3.2
import utility
def add(a, b):
return a + b
print(add(12, 12))
print(utility.add(12, 12))
Compare an unqualified local function with utility.add() to see how a module namespace prevents the definitions from shadowing one another. section 2.3.2



