Skip to content

Global scoping when shadowing local names in class definitions #79142

Closed

Description

@multun
mannequin
BPO 34961
Nosy @terryjreedy, @swarmer, @seirl, @multun
Files
  • weird_class_scope.py: A broader example, demonstrating it only works with classes, and the scope the variable comes from is global
  • 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 2018-10-12.20:56:52.152>
    created_at = <Date 2018-10-11.19:15:04.232>
    labels = ['interpreter-core', '3.8', 'type-bug', '3.7', 'invalid']
    title = 'Global scoping when shadowing local names in class definitions'
    updated_at = <Date 2018-10-12.20:56:52.142>
    user = 'https://github.com/multun'

    bugs.python.org fields:

    activity = <Date 2018-10-12.20:56:52.142>
    actor = 'multun'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-12.20:56:52.152>
    closer = 'multun'
    components = ['Interpreter Core']
    creation = <Date 2018-10-11.19:15:04.232>
    creator = 'multun'
    dependencies = []
    files = ['47862']
    hgrepos = []
    issue_num = 34961
    keywords = []
    message_count = 4.0
    messages = ['327547', '327588', '327607', '327612']
    nosy_count = 4.0
    nosy_names = ['terry.reedy', 'anton.barkovsky', 'antoine.pietri', 'multun']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34961'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    Activity

    1. multun commented on Oct 11, 2018

      multunmannequin
      MannequinAuthor

      Hello,

      >>> a="global"
      >>> def func(a):
      ...     class wtf():
      ...             a=a
      ...     return wtf
      ... 
      >>> func("local").a
      'global'
      >>> def func2(a):
      ...     class wtf():
      ...             b=a
      ...     return wtf
      ... 
      >>> func2("local").b
      'local'

      Is this behavior legit ?

      Best regards,

      --
      Victor "multun" Collod

    2. added
      3.7 (EOL)
      3.8 (EOL)
      interpreter-core
      type-bug
      on Oct 11, 2018
    3. seirl commented on Oct 12, 2018

      seirlmannequin
      Mannequin

      Tracking down the issue a bit further:

          a = 10
      
      
          def main():
              a = 42
      
              class wtf():
                  print(a)
      
              class wtf2():
                  print(a)
                  a = 2
      
      
          main()

      prints:

      42
      10
      

      It seems that when there is an assignation in the class body, prior usages of the variables in the class body are done by loading the variable using LOAD_NAME instead of LOAD_CLASSDEREF:

      Disassembly of <code object wtf at 0x7fa1b8b01390, file "lolmul.py", line 7>:
      [...]

      8 8 LOAD_NAME 3 (print)
      10 LOAD_CLASSDEREF 0 (a)
      12 CALL_FUNCTION 1
      14 POP_TOP
      16 LOAD_CONST 1 (None)
      18 RETURN_VALUE

      Disassembly of <code object wtf2 at 0x7fa1b8b018a0, file "lolmul.py", line 10>:
      [...]

      11 8 LOAD_NAME 3 (print)
      10 LOAD_NAME 4 (a)
      12 CALL_FUNCTION 1
      14 POP_TOP

      12 16 LOAD_CONST 1 (2)
      18 STORE_NAME 4 (a)
      20 LOAD_CONST 2 (None)
      22 RETURN_VALUE

    4. terryjreedy commented on Oct 12, 2018

      @terryjreedy
      Member

      Questions about whether basic syntax behavior is correct should be asked elsewhere, such as python-list. A proper answer requires a careful reading of the doc and a non-trivial answer. Someone on python-list should do that.

      Classes and scoping are known to have some non-intuitive corner cases. Quite aside from the fact that the default assumption should be that the implementation of heavily used syntax is correct, my memory is that what you see is correct. But I have not read the appropriate parts of the doc for a few years.

      PS. 3.4 and 3.5 currently only get security fixes. The same will be true of 3.6 sometime next year.

    5. multun commented on Oct 12, 2018

      multunmannequin
      MannequinAuthor

      Closing, this is documented, and as such most likely intended.

      "A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace."
      https://docs.python.org/3.7/reference/executionmodel.html#builtins-and-restricted-execution

    6. 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

      3.7 (EOL)3.8 (EOL)interpreter-coretype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions