Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that locals() may return globals() #76594

Closed
stevendaprano opened this issue Dec 23, 2017 · 5 comments
Closed

Document that locals() may return globals() #76594

stevendaprano opened this issue Dec 23, 2017 · 5 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@stevendaprano
Copy link
Member

BPO 32413
Nosy @brettcannon, @rhettinger, @stevendaprano, @vadmium, @srinivasreddy, @miss-islington
PRs
  • bpo-32413: Add documentation that at the module level, locals(), globals() are the same dictionary #5004
  • [3.7] bpo-32413: Add documentation that at the module level, locals(), globals() are the same dictionary (GH-5004) #12664
  • 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 2019-04-02.18:15:54.835>
    created_at = <Date 2017-12-23.05:52:42.322>
    labels = ['3.8', 'type-feature', '3.7', 'docs']
    title = 'Document that locals() may return globals()'
    updated_at = <Date 2019-04-02.18:15:54.834>
    user = 'https://github.com/stevendaprano'

    bugs.python.org fields:

    activity = <Date 2019-04-02.18:15:54.834>
    actor = 'brett.cannon'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-04-02.18:15:54.835>
    closer = 'brett.cannon'
    components = ['Documentation']
    creation = <Date 2017-12-23.05:52:42.322>
    creator = 'steven.daprano'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32413
    keywords = ['patch']
    message_count = 5.0
    messages = ['308947', '308949', '309004', '309391', '339359']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'rhettinger', 'steven.daprano', 'docs@python', 'martin.panter', 'thatiparthy', 'miss-islington']
    pr_nums = ['5004', '12664']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32413'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    @stevendaprano
    Copy link
    Member Author

    The obvious documentation for locals() fails to mention that when called from the top level of a module (outside of a function or class body) it returns the same dict as globals().

    https://docs.python.org/2/library/functions.html#locals

    @stevendaprano stevendaprano added 3.7 (EOL) end of life 3.8 only security fixes labels Dec 23, 2017
    @stevendaprano stevendaprano added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Dec 23, 2017
    @rhettinger
    Copy link
    Contributor

    FWIW, this isn't a quirk of how locals() works. Instead, it reflects the more interesting reality that at the top level, locals and globals *are* the same dictionary. Also, that is not the only occurrence -- if exec() is called with only one dictionary, that dict is used for both locals and globals.

    @srinivasreddy
    Copy link
    Mannequin

    srinivasreddy mannequin commented Dec 24, 2017

    Done. exec(...)'s documentation covers raymond's comment.

    @vadmium
    Copy link
    Member

    vadmium commented Jan 2, 2018

    FWIW a few years ago I wrote a patch for bpo-17546 that documents three personalities of “locals”, including:

    • At the module level, the dictionary returned is the global symbol table, also returned by :func:`globals`.

    @miss-islington
    Copy link
    Contributor

    New changeset ef516d1 by Miss Islington (bot) in branch '3.7':
    bpo-32413: Add documentation that at the module level, locals(), globals() are the same dictionary (GH-5004)
    ef516d1

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants