Skip to content

UnboundLocalError in nested function #53295

Description

@AndreasHofmeister
BPO 9049
Nosy @mdickinson

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-06-21.19:52:40.988>
created_at = <Date 2010-06-21.19:46:37.946>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'UnboundLocalError in nested function'
updated_at = <Date 2010-06-22.08:26:14.701>
user = 'https://bugs.python.org/AndreasHofmeister'

bugs.python.org fields:

activity = <Date 2010-06-22.08:26:14.701>
actor = 'mark.dickinson'
assignee = 'none'
closed = True
closed_date = <Date 2010-06-21.19:52:40.988>
closer = 'mark.dickinson'
components = ['Interpreter Core']
creation = <Date 2010-06-21.19:46:37.946>
creator = 'Andreas Hofmeister'
dependencies = []
files = []
hgrepos = []
issue_num = 9049
keywords = []
message_count = 4.0
messages = ['108306', '108309', '108337', '108345']
nosy_count = 2.0
nosy_names = ['mark.dickinson', 'Andreas Hofmeister']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue9049'
versions = ['Python 2.6', 'Python 2.5']

Activity

  1. AndreasHofmeister commented on Jun 21, 2010

    AndreasHofmeistermannequin
    MannequinAuthor

    Description:
    An unexpected UnboundLocalError is produced when assigning a value to a variable inside a nested function. The first assignment to the variable is in the enclosing function.

    Example:
    def x():
    a = False
    def y():
    print a
    a = True
    return y

    Calling x()() produces an UnboundLocalError on the 'print a' line.
    If the 'a = True' line is removed, no error occurs.

    Tested with:

    • 2.5.1
    • 2.6.5

    Keywords:
    Nested function, UnboundLocalError, variable assignment

    Thank you for your attention

  2. mdickinson commented on Jun 21, 2010

    @mdickinson
    Member

    This isn't a bug; it's by design.

    Because there's an assignment to 'a' in the function 'y', 'a' is considered local to that function. (It doesn't matter where the assignment happens within the function; the presence of an assignment anywhere is enough to make 'a' local for the entirety of 'y'.)

    This is described in the reference manual at:

    http://docs.python.org/reference/executionmodel.html#naming-and-binding

    See the paragraph beginning:

    "If a name binding operation occurs anywhere within a code block, "

  3. AndreasHofmeister commented on Jun 22, 2010

    AndreasHofmeistermannequin
    MannequinAuthor

    Thank you for your assistance. I apologize for not examining the reference manual closely.
    Is there any way to produce the desired behavior? I currently work around the local name binding like this:

    def x():
         a = [False]
         def y():
             print a[0]
             a[0] = True
         return y

    However, using a list here seems awkward.
    Thank you for your attention.

  4. mdickinson commented on Jun 22, 2010

    @mdickinson
    Member

    Is there any way to produce the desired behavior?

    Not directly, in Python 2.x. (But there's the 'nonlocal' keyword in 3.x.) There are various workarounds, but what's best depends on what you're doing. The python-list mailing list is probably a better place to get answers.

  5. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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