Skip to content

Strange behaviour for equal variable and function name #132583

@SirJoeth3rd

Description

@SirJoeth3rd

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

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions