Skip to content

Using the undefined variable num, the code num += 5 inside of a function gets a different error message #137868

@hyperkai

Description

@hyperkai

Bug report

Bug description:

Using the undefined variable num, the code print(num) outside and inside of a function and num += 5 outside of a function gets the same error messages as shown below:

# num = 10

print(num)
# NameError: name 'num' is not defined

num += 5
# NameError: name 'num' is not defined
def func():
    # num = 10
    print(num)

func()
# NameError: name 'num' is not defined

But using the undefined variable num, the code num += 5 inside of a function gets the different error message as shown below:

def func():
    # num = 10
    num += 5

func()
# UnboundLocalError: cannot access local variable 'num' where it is not associated with a value

So, the error message just above should also be the same as shown below:

NameError: name 'num' is not defined

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions