-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Closed as not planned
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Hello, at my job we recently encountered a strange bug that resulted in a UnboundLocalError.
The following is from a ipython terminal.
In [2]: def example_one():
...: x = x()
In [3]: dis.dis(example_one)
2 0 LOAD_FAST 0 (x)
2 CALL_FUNCTION 0
4 STORE_FAST 0 (x)
6 LOAD_CONST 0 (None)
8 RETURN_VALUE
In [4]: def example_two():
...: x = y()
In [5]: dis.dis(example_two)
2 0 LOAD_GLOBAL 0 (y)
2 CALL_FUNCTION 0
4 STORE_FAST 0 (x)
6 LOAD_CONST 0 (None)
8 RETURN_VALUE
As you can see in example_one python is attempting to fetch the x function locally. Wouldn't expected behaviour be like example_two where the function is loaded from global and the name is then reassigned in the local scope?
It's obviously stupid rare (and bad practice) to shadow a function name like this, but still worth a mention.
CPython versions tested on:
3.10, 3.14
Operating systems tested on:
Windows, Linux
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error