classification
Title: Documentation of globals() and locals() should be improved
Type: Stage:
Components: Documentation Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Zahari.Dim, docs@python, martin.panter, r.david.murray, terry.reedy
Priority: normal Keywords: patch

Created on 2013-11-23 16:51 by Zahari.Dim, last changed 2015-05-18 22:46 by martin.panter.

Files
File name Uploaded Description Edit
globals-copy.patch martin.panter, 2014-12-17 02:53 review
Messages (8)
msg204052 - (view) Author: Zahari Dim (Zahari.Dim) Date: 2013-11-23 16:51
The globals() notification states:

Return a dictionary representing the current global symbol table.[...]

This doc and the fact that globals() is called as a function made me think that globals() returns a copy of the global namespace dict, rather than an object that could be used to actually modify the namespace. I don't find obvious the meaning of "representing" in this context.

This of course led to a very nasty and sneaky bug in my code.

The docs of locals() don't seem clear to me either, thought at least it seems to imply that it is actually modifying the namespace.
msg204055 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-11-23 17:06
We've tried improving the locals docs several times.  Modifying it is not "safe", in the sense that what happens when you do is not defined by the language.  Which locals docs were you looking at?

I agree that 'representing' is not as clear as would be optimal in the globals description.  Again, which bit of the docs are you looking at?
msg204128 - (view) Author: Zahari Dim (Zahari.Dim) Date: 2013-11-23 23:37
I am looking at the docs of the built-in functions:

http://docs.python.org/2/library/functions.html
msg204769 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-11-30 03:25
In my opinion, vague ideas like this one should go to python-ideas first.

I agree with David that globals() and locals() are separate issues. Since there have been and perhaps still are locals() doc issues, I will take this one to be about 'globals()'.

A 'symbol table' is a mapping between 'symbols' and 'values'. I presume such were originally implemented as a concrete list of pairs, but the term is now is an abstraction. In the Python context, I think 'namespace' would be a better word, but I would not make the change without getting other opinions, such as on python-ideas. 

The [...] that you omitted says "This is always the dictionary of the current module (...)." I cannot interpret 'is' to mean a copy. So I think "[concrete] dictionary represents [abstract] symbol table [namespace]" is fine.
msg204777 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2013-11-30 05:31
How about swapping the two sentences for globals() then:

“Returns the dictionary of the module . . . This represents the symbol table . . .”

I thought the current locals() entry is fairly clear. It actually says _not_ to modify the dictionary!
msg232796 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-12-17 02:51
Here is a patch with my suggestion. It also now refers to _the_ dictionary rather than just _a_ dictionary, and drops the word “current”, so that it does not sound like saving a snapshot.
msg243430 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-05-17 22:13
My succinct version:

Return the dictionary implementing the current module namespace. For code within functions, this is set when the function is defined and remains the same regardless of where a function is called.
msg243547 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-05-18 22:46
Terry’s “dictionary implementing the namespace” version would work for me.
History
Date User Action Args
2015-05-18 22:46:45martin.pantersetmessages: + msg243547
2015-05-17 22:13:16terry.reedysetmessages: + msg243430
versions: + Python 3.5, - Python 3.3
2014-12-17 02:53:53martin.pantersetfiles: - globals-copy.patch
2014-12-17 02:53:41martin.pantersetfiles: + globals-copy.patch
2014-12-17 02:51:42martin.pantersetfiles: + globals-copy.patch
keywords: + patch
messages: + msg232796
2013-11-30 05:31:16martin.pantersetmessages: + msg204777
2013-11-30 03:25:54terry.reedysetnosy: + terry.reedy

messages: + msg204769
versions: + Python 2.7, Python 3.3, Python 3.4
2013-11-24 00:01:25martin.pantersetnosy: + martin.panter
2013-11-23 23:37:04Zahari.Dimsetmessages: + msg204128
2013-11-23 17:06:39r.david.murraysetnosy: + r.david.murray
messages: + msg204055
2013-11-23 16:51:01Zahari.Dimcreate