classification
Title: Collections - ChainMap - Documentation example wrong order line
Type: compile error Stage: resolved
Components: Documentation Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Marcos Soutullo, docs@python, rhettinger
Priority: low Keywords:

Created on 2017-07-29 12:24 by Marcos Soutullo, last changed 2017-09-07 21:26 by rhettinger. This issue is now closed.

Messages (3)
msg299465 - (view) Author: Marcos Soutullo (Marcos Soutullo) Date: 2017-07-29 12:24
Hello,

I have been taking a look into your ChainMap collections documentation (https://docs.python.org/3.6/library/collections.html?highlight=collections#collections.ChainMap), specifically the third code example on "8.3.1.1. ChainMap Examples and Recipes"
that clearly describe and illustrate a use case for the ChainMap class. However, I found a very small code issue in regards to how the sample code (line 7) is presented to the reader. Please refer to the code compilation below:

Line 7 >>> d['x']                # Get first key in the chain of contexts
raise KeyError(key)
KeyError: 'x'
Line 8 >>> d['x'] d['x'] = 1            # Set value in current context

The key named 'x' had not been initialised yet and of course, the compiler is complaining. It should be the other way around.

Line 8 >>> d['x'] = 1   # Set value in current context
Line 7 >>> d['x']       # Get first key in the chain of contexts


Many thanks for the great work you do with Python,

Marcos S.
msg299478 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-07-29 17:03
-0

The code block was originally intended to just be a table of patterns, not meant to be executed sequentially.  It followed the traditional presentation order (__getitem__, __setitem__, followed by __delitem__).

If we do change this, then the subsequent "k in d" example should also be changed to "'x' in d" because k isn't defined.
msg299484 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-07-29 19:18
> Many thanks for the great work you do with Python,

Thanks for the kudos.  Also, thanks for the close reading of the docs.
History
Date User Action Args
2017-09-07 21:26:09rhettingersetstatus: open -> closed
stage: resolved
2017-07-29 19:18:46rhettingersetmessages: + msg299484
2017-07-29 17:03:26rhettingersetpriority: normal -> low

nosy: + rhettinger
messages: + msg299478

assignee: docs@python -> rhettinger
resolution: not a bug
2017-07-29 12:24:17Marcos Soutullocreate