classification
Title: Document that locals() may return globals()
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: brett.cannon, docs@python, martin.panter, miss-islington, rhettinger, steven.daprano, thatiparthy
Priority: normal Keywords: patch

Created on 2017-12-23 05:52 by steven.daprano, last changed 2019-04-02 18:15 by brett.cannon. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5004 merged thatiparthy, 2017-12-24 17:31
PR 12664 merged miss-islington, 2019-04-02 17:59
Messages (5)
msg308947 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-12-23 05:52
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
msg308949 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-12-23 08:12
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.
msg309004 - (view) Author: Srinivas Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) (thatiparthy) * Date: 2017-12-24 17:42
Done. exec(...)'s  documentation covers raymond's comment.
msg309391 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-01-02 19:37
FWIW a few years ago I wrote a patch for Issue 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`.
msg339359 - (view) Author: miss-islington (miss-islington) Date: 2019-04-02 18:14
New changeset ef516d11c1a0f885dba0aba8cf5366502077cdd4 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)
https://github.com/python/cpython/commit/ef516d11c1a0f885dba0aba8cf5366502077cdd4
History
Date User Action Args
2019-04-02 18:15:54brett.cannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-04-02 18:14:56miss-islingtonsetnosy: + miss-islington
messages: + msg339359
2019-04-02 17:59:08miss-islingtonsetpull_requests: + pull_request12592
2019-04-01 21:54:37brett.cannonsetnosy: + brett.cannon
2018-01-02 19:37:13martin.pantersetnosy: + martin.panter
messages: + msg309391
2017-12-24 17:42:42thatiparthysetnosy: + thatiparthy
messages: + msg309004
2017-12-24 17:31:13thatiparthysetkeywords: + patch
stage: patch review
pull_requests: + pull_request4893
2017-12-23 08:12:02rhettingersetmessages: + msg308949
2017-12-23 08:09:45rhettingersetnosy: + rhettinger
2017-12-23 05:52:42steven.dapranocreate