GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Better specify number of nested scopes · Issue #70393 · python/cpython

Better specify number of nested scopes #70393

New issue
Copy link
New issue
Copy link
Closed
Closed
Better specify number of nested scopes#70393
Copy link
Assignees
Mariatta
Labels
3.10 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifedocsDocumentation in the Doc dirDocumentation in the Doc dirtype-featureA feature request or enhancementA feature request or enhancement

Description

RoscoeRHiggins
mannequin
opened on Jan 26, 2016
Issue body actions
BPO 26205
Nosy @terryjreedy, @ezio-melotti, @eryksun, @vedgar, @Mariatta, @csabella, @miss-islington, @nanjekyejoannah
PRs
  • bpo-26205: Specify the number of nested scopes #21324
  • [3.9] bpo-26205: Specify the number of nested scopes (GH-21324) #21341
  • [3.8] bpo-26205: Specify the number of nested scopes (GH-21324) #21342
  • 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/Mariatta'
    closed_at = None
    created_at = <Date 2016-01-26.02:16:27.767>
    labels = ['type-feature', '3.8', '3.9', '3.10', 'docs']
    title = 'Better specify number of nested scopes'
    updated_at = <Date 2020-11-04.21:36:36.791>
    user = 'https://bugs.python.org/RoscoeRHiggins'

    bugs.python.org fields:

    activity = <Date 2020-11-04.21:36:36.791>
    actor = 'brett.cannon'
    assignee = 'Mariatta'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2016-01-26.02:16:27.767>
    creator = 'Roscoe R. Higgins'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26205
    keywords = ['patch']
    message_count = 20.0
    messages = ['258941', '258964', '258978', '258980', '259228', '340000', '373022', '373048', '373049', '373055', '373056', '373058', '373059', '373061', '373062', '373063', '373068', '373085', '373087', '373125']
    nosy_count = 11.0
    nosy_names = ['terry.reedy', 'ezio.melotti', 'docs@python', 'eryksun', 'veky', 'Hibou57', 'Roscoe R. Higgins', 'Mariatta', 'cheryl.sabella', 'miss-islington', 'nanjekyejoannah']
    pr_nums = ['21324', '21341', '21342']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue26205'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    • PR: gh-70393: Clarify mention of "middle" scope #98839
    • PR: [3.11] gh-70393: Clarify mention of "middle" scope (GH-98839) #100186
    • PR: [3.10] gh-70393: Clarify mention of "middle" scope (GH-98839) #100187

    Activity

    Next
    1. RoscoeRHiggins commented on Jan 26, 2016

      @RoscoeRHiggins
      RoscoeRHigginsmannequin
      on Jan 26, 2016
      MannequinAuthor
      More actions

      In chapter 9. Classes of the Python3.5 documentation it says:

      "At any time during execution, there are at least three nested scopes whose namespaces are directly accessible:",

      followed by a list containing 4 items.
      Further down a middle scope is mentioned (although mentioned by name). This was confusing for a while.

    2. RoscoeRHiggins
      added
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Jan 26, 2016
    3. RoscoeRHiggins
      assigned
      docspython
      on Jan 26, 2016
    4. RoscoeRHiggins
      added
      docsDocumentation in the Doc dirDocumentation in the Doc dir
      on Jan 26, 2016
    5. brettcannon commented on Jan 26, 2016

      @brettcannon
      brettcannon
      on Jan 26, 2016
      Member
      More actions

      https://docs.python.org/3/tutorial/classes.html for the docs that Roscoe is talking about.

      So the sentence is technically correct, it just takes careful reading to grasp what's being said. There are "at least three nested scopes", but there can be *up to* four scopes. Since "the scopes of any enclosing functions" is not necessarily true for all code scopes, you end up with at least three, but possibly four scopes.

      Obviously the wording could be clearer, so if you want to sign the CLA, Roscoe, and propose a rewording that would be appreciated!

    6. Hibou57 commented on Jan 26, 2016

      @Hibou57
      Hibou57mannequin
      on Jan 26, 2016
      Mannequin
      More actions

      Better at least two, if I'm not wrong: the innermost scope may be the module's scope. So there is always at least the module scope and the built‑ins scope, at least two up to four.

      (if I have not overlooked something)

    7. brettcannon commented on Jan 26, 2016

      @brettcannon
      brettcannon
      on Jan 26, 2016
      Member
      More actions

      It depends on how you want to view things as to whether you can claim there are two or three scopes for module-level code. While it's true that the local scope operates just like global scope, to Python it's more like local == global rather than the local scope simply doesn't exist (hence why locals() never throws an exception saying the scope doesn't exist but instead is the same as globals()).

      The "three scope" phrasing also predates nested scopes from back when Python had its LGB scoping rules: Local-Global-Builtin. Back then we just said Python had three scopes and left it at that since it was basically always true and didn't confuse anyone.

      At this point I'm fine with just removing the number from the sentence and saying something like "At any time during execution, there are various nested scopes whose namespaces are directly accessible:".

    8. brettcannon
      removed
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Jan 26, 2016
    9. terryjreedy commented on Jan 30, 2016

      @terryjreedy
      terryjreedy
      on Jan 30, 2016
      Member
      More actions

      Would 'three or more' be any clearer than 'at least three'? They mean the same, but the first seems better to me in this context.

      The real problem with this section are a) the use of Guido's first person 'I' and b) statements that were not changed when nested scope were added, but should have been.

      "If a name is declared global, then all references and assignments go directly to the middle scope containing the module’s global names."

      The global scope is no longer the middle scope. Roscoe pointed at this. With that removed, the sentence says that if a name is declared global, assignments go to global scope. This would be more meaningful if prefixed by a revised version of the following, which is several paragraphs down.

      "A special quirk of Python is that – if no global statement is in effect – assignments to names always go into the innermost scope."

      The special quirk part should go; 'global' would now have to be 'global or nonlocal', but I now think the following, preceeding the revised 'global' sentence above, would be better.

      "By default, assignments to names always go into the innermost, local, namespace."

      In other words, I think the sentence Roscoe flagged is the least of the problems with this section.

    10. ezio-melotti
      added
      type-featureA feature request or enhancementA feature request or enhancement
      on Mar 12, 2016
    11. csabella commented on Apr 11, 2019

      @csabella
      csabella
      on Apr 11, 2019
      Contributor
      More actions

      Assigning to @Mariatta for the sprints.

    12. csabella
      added
      3.7 (EOL)end of lifeend of life
      3.8 (EOL)end of lifeend of life
      on Apr 11, 2019
    13. csabella
      assigned
      Mariatta
      and unassigned
      docspython
      on Apr 11, 2019
    14. 15 remaining items

    15. nanjekyejoannah commented on Jul 6, 2020

      @nanjekyejoannah
      nanjekyejoannah
      on Jul 6, 2020
      Contributor
      More actions

      New changeset 3f4a9fd by Miss Islington (bot) in branch '3.9':
      bpo-26205: Specify the number of nested scopes (GH-21324) (GH-21341)
      3f4a9fd

    16. vedgar commented on Jul 6, 2020

      @vedgar
      vedgarmannequin
      on Jul 6, 2020
      Mannequin
      More actions

      Well, then "what name is in which namespace" is relative to which function we're considering. In my example above, imagine aa has x declared, bb has y, and cc has z. Then y and z are in the same namespace when we look from the perspective of aa, but they are not in the same namespace from the perspective of bb. Even worse, cc sees z but doesn't see y. How can they be in the same namespace then?

      I always thought about namespaces as mappings from names to objects, independent of perspective. Whether two names are in the same namespace, should be a question with an objective answer.

    17. terryjreedy commented on Jul 6, 2020

      @terryjreedy
      terryjreedy
      on Jul 6, 2020
      Member
      More actions

      Since you ask, here is a extended summary of namespaces. There is one built-in namespace. There is one global namespace for each module, which is also the local namespace for top level code in that module. There is one local namespace for each class and function. There is one nonlocal namespace for each nested function. "At any time during execution" (the beginning of the doc sentence targeted by this issue), there are 3 or maybe 4 namespaces accessible with simple undotted names.

      Module global and class local namespaces, including nested classes, are accessible from without the object with dotted names. There are rules for class and method code about access to superclass namespaces. Function locals are not necessarily accessible with standard python code, but at least in cpython, local names can be accessed via code objects. Default parameter values and current nonlocal values, when set, can be accessed via function objects. Dotted names are discussed elsewhere in the tutorial, while function and code object introspection is out of scope there. (And off topic for this issue ;-).

    18. terryjreedy commented on Jul 6, 2020

      @terryjreedy
      terryjreedy
      on Jul 6, 2020
      Member
      More actions

      Besides the sentence now revised, the initial post referenced confusion with 'middle scope' in "If a name is declared global, then all references and assignments go directly to the middle scope containing the module's global names."

      This has not been directly discussed that I could find, but it seems inappropriate both at toplevel, where globals = locals, and in nested functions, where nonlocals are also 'middle'. Maybe replace "the middle scope containing the module's global names" with "the module's global namespace". The rest of this paragraph and the next could be reviewed.

    19. eryksun commented on Jul 6, 2020

      @eryksun
      eryksun
      on Jul 6, 2020
      Contributor
      More actions

      There is one local namespace for each class and function. There is one
      nonlocal namespace for each nested function.

      In the first sentence, replace "class" with "class statement" and "function" with "function call". The second sentence could use "nested-function call", but maybe rewrite it as, "There is one nonlocal namespace when a nested function is called."

    20. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    21. hauntsaninja
      added a commit that references this issue on Oct 29, 2022

      pythongh-70393: Clarify mention of "middle" scope

      bfbad0c
    22. hauntsaninja commented on Oct 29, 2022

      @hauntsaninja
      hauntsaninja
      on Oct 29, 2022
      Contributor
      More actions

      #98839 for clarifying "middle" scope

    23. JelleZijlstra
      added a commit that references this issue on Dec 12, 2022

      gh-70393: Clarify mention of "middle" scope (#98839)

      70be5e4
    24. miss-islington
      added 4 commits that reference this issue on Dec 12, 2022

      pythongh-70393: Clarify mention of "middle" scope (pythonGH-98839)

      f9ff179

      pythongh-70393: Clarify mention of "middle" scope (pythonGH-98839)

      5cedf5e

      gh-70393: Clarify mention of "middle" scope (GH-98839)

      71b0326

      gh-70393: Clarify mention of "middle" scope (GH-98839)

      eece1bd
    25. carljm
      added a commit that references this issue on Dec 14, 2022

      Merge branch 'main' into typedocs

      53d33a1
    26. hauntsaninja
      closed this as completedon Dec 24, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    • @Mariatta
      Mariatta

    Labels

    3.10 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifedocsDocumentation in the Doc dirDocumentation in the Doc dirtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.