python / cpython

The Python programming language
https://www.python.org
Other
63.16k stars 30.25k forks source link

Improve scope example in Tutorial, chapter 9 #82856

Open 79033bd9-6771-4faa-9307-806d0dc7dcba opened 4 years ago

79033bd9-6771-4faa-9307-806d0dc7dcba commented 4 years ago
BPO 38675
Nosy @terryjreedy

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: ```python assignee = None closed_at = None created_at = labels = ['type-feature', '3.8', 'docs'] title = 'Improve scope example in Tutorial, chapter 9' updated_at = user = 'https://bugs.python.org/DavidGoldsmith' ``` bugs.python.org fields: ```python activity = actor = 'terry.reedy' assignee = 'docs@python' closed = False closed_date = None closer = None components = ['Documentation'] creation = creator = 'David Goldsmith' dependencies = [] files = [] hgrepos = [] issue_num = 38675 keywords = [] message_count = 2.0 messages = ['355908', '356272'] nosy_count = 3.0 nosy_names = ['terry.reedy', 'docs@python', 'David Goldsmith'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue38675' versions = ['Python 3.8'] ```

79033bd9-6771-4faa-9307-806d0dc7dcba commented 4 years ago

In The Python Tutorial, at the end of Section 9.2.1 "Scopes and Namespaces Example," there occurs the statement: "You can also see that there was no previous binding for spam before the global assignment." Indeed, one can "virtually see" this by mentally analyzing what the code is doing--if one adequately understood the exposition given in Section 9.2--but, unless it is to be understood by an omission in the example code's output, which is a conclusion I myself am missing, that example code's output does not explicitly validate this claim...and yet, with the addition of just one line to the code, the claim can be shown explicitly: simply copy the line of code:

print("In global scope:", spam)

to precede, as well as follow (as it currently does) the line:

scope_test()

and the code output changes to:

>>> print("In global scope:", spam)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> scope_test()
After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
>>> print("In global scope:", spam)
In global scope: global spam  

This does _explicitly_ show that "there was no previous binding for spam before the global assignment": I respectfully suggest that this line be added to the code and that the code's quoted output be suitably updated as well.

terryjreedy commented 4 years ago

To me, this is a plausible addition

slateny commented 2 years ago

If a crash is allowed, then the entire section would need to be one of those executable blocks, otherwise midway through I think it'd stop printing the rest of the example. Or, maybe the first print statement can be in a try-except block since it was already covered in section 8, then it wouldn't need to be in an executable block