Skip to content

weird namespace phenomenon #35220

Closed
Closed

Description

@anonymous
mannequin
BPO 463640
Nosy @tim-one

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 = 'https://github.com/tim-one'
closed_at = <Date 2001-09-21.20:15:03.000>
created_at = <Date 2001-09-21.18:26:45.000>
labels = ['interpreter-core', 'invalid']
title = 'weird namespace phenomenon'
updated_at = <Date 2001-09-21.20:15:03.000>
user = 'https://bugs.python.org/anonymous'

bugs.python.org fields:

activity = <Date 2001-09-21.20:15:03.000>
actor = 'tim.peters'
assignee = 'tim.peters'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2001-09-21.18:26:45.000>
creator = 'anonymous'
dependencies = []
files = []
hgrepos = []
issue_num = 463640
keywords = []
message_count = 2.0
messages = ['6636', '6637']
nosy_count = 1.0
nosy_names = ['tim.peters']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue463640'
versions = []

Activity

  1. nobody commented on Sep 21, 2001

    nobodymannequin
    Mannequin
    # == module f.py
    y = 10
    
    def g():
    
      if y:
        y = 100
    
      return y

    # == module h.py

    import f
    print f.g()

    # == plop these two modules into the same directory
    and 'cd' to that directory.

    When you run 'python h.py' at the command line, line 6
    of f.py (if y:)
    creates a problem due to f being supposedly undefined.
    The error is
    registered as NameError or UnboundLocalError depending
    on what version
    of the python core you're running. Python 2.1.x says
    y is being
    referenced before assignment, yet if you change the
    _next_ line to read
    "z = 100" rather than "y = 100", the whole thing runs
    fine, printing 10
    (the value of f.y) at the end as expected.

    This is at the very least an extreme logic quirk in
    how python namespaces
    work, and at worst a bug badly in need of correction
    (since vsn 1.5.2 or
    before).

  2. tim-one commented on Sep 21, 2001

    @tim-one
    Member

    Logged In: YES
    user_id=31435

    Whether a given name is local or global in a given scope is
    a property determined at compile-time, not at runtime. See
    the Reference Manual for details. In brief, if a name is
    bound ("assigned to") *anywhere* in the body of a function,
    that name is local throughout the entire function. You
    assign to y in the body of g(), therefore y is a local
    variable everywhere in g(), and you reference y's value in g
    () before giving it a value (hence it's an unbound local at
    the time you first reference it, hence UnboundLocalError in
    recent Pythons). The y in the body of g() has nothing to
    do with the module-level vrbl y. If you intended to
    reference the module-level vrbl y, then you need to put

    global y

    as the first statement in g().

    But it's impossible for me to guess what you intended --
    and it's also impossible for Python.

    If you're familiar with C, it's exactly the same as

    int y = 10;
    
    int g() {int y; if (y) y = 100; return y;}

    except that in C you'd get random crap out of that function
    (due to referencing an uninitialized auto) instead of a
    reliable exception. The difference is that Python doesn't
    have explict declarations for local variables, but the fact
    that you assigned to y within the body of g makes y a local
    just as much as y is function-local in the C example above.

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

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions