classification
Title: Inconsistency concerning nested scopes
Type: enhancement Stage: needs patch
Components: Documentation Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Mariatta Nosy List: Hibou57, Mariatta, Roscoe R. Higgins, brett.cannon, cheryl.sabella, docs@python, ezio.melotti, terry.reedy
Priority: normal Keywords:

Created on 2016-01-26 02:16 by Roscoe R. Higgins, last changed 2019-04-11 22:41 by cheryl.sabella.

Messages (6)
msg258941 - (view) Author: Roscoe R.Higgins (Roscoe R. Higgins) Date: 2016-01-26 02:16
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.
msg258964 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-01-26 17:37
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!
msg258978 - (view) Author: Yannick Duchêne (Hibou57) Date: 2016-01-26 22:07
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)
msg258980 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-01-26 22:21
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:".
msg259228 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-01-30 01:39
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.
msg340000 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-04-11 22:41
Assigning to @Mariatta for the sprints.
History
Date User Action Args
2019-04-11 22:41:55cheryl.sabellasetversions: + Python 3.7, Python 3.8, - Python 2.7, Python 3.5, Python 3.6
nosy: + Mariatta, cheryl.sabella

messages: + msg340000

assignee: docs@python -> Mariatta
2016-03-12 09:34:15ezio.melottisetnosy: + ezio.melotti
type: enhancement
2016-01-30 01:39:40terry.reedysetnosy: + terry.reedy
messages: + msg259228
2016-01-26 22:22:08brett.cannonsetstage: needs patch
type: behavior -> (no value)
versions: + Python 2.7, Python 3.6
2016-01-26 22:21:50brett.cannonsetmessages: + msg258980
2016-01-26 22:07:13Hibou57setnosy: + Hibou57
messages: + msg258978
2016-01-26 17:37:16brett.cannonsetnosy: + brett.cannon
messages: + msg258964
2016-01-26 02:16:27Roscoe R. Higginscreate