Title: Operator Documentation Example doesn't work
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.3
Status: closed Resolution: duplicate
Dependencies: Superseder: operator.setitem example no longer works in Python 3 due to lazy map
View: 22180
Assigned To: docs@python Nosy List: asvetlov, docs@python, eric.araujo, ezio.melotti,, rhettinger, silent.bat
Priority: normal Keywords: patch

Created on 2014-02-12 10:40 by silent.bat, last changed 2015-01-04 22:17 by benjamin.peterson. This issue is now closed.

File name Uploaded Description Edit
operator.patch, 2014-02-12 12:41 review
Messages (4)
msg211079 - (view) Author: silentbat (silent.bat) Date: 2014-02-12 10:40
In the Docs for Python3.3

the following example doesn't work.
msg211088 - (view) Author: Gareth Rees ( * (Python triager) Date: 2014-02-12 12:38
The failing example is:

    d = {}
    keys = range(256)
    vals = map(chr, keys)
    map(operator.setitem, [d]*len(keys), keys, vals)   

which works in Python 2 where map returns a list, but not in Python 3 where map returns an iterator.

Doc/library/operator.rst follows the example with this note:

    .. XXX: find a better, readable, example

Additional problems with the example:

1. It's poorly motivated because a dictionary comprehension would be simpler and shorter:

    d = {i: chr(i) for i in range(256)}

2. It's also unclear why you'd need this dictionary when you could just call the function chr (but I suppose some interface might require a dictionary rather than a function).

3. To force the map to be evaluated, you need to write list(map(...)) which allocates an unnecessary list object and then throws it away. To avoid the unnecessary allocation you could use the "consume" recipe from the itertools documentation and write collections.deque(map(...), maxlen=0) but this is surely too obscure to use as an example.

I had a look through the Python sources, and made an Ohloh Code search for "operator.setitem" and I didn't find any good examples of its use, so I think the best thing to do is just to delete the example.

msg233418 - (view) Author: Gareth Rees ( * (Python triager) Date: 2015-01-04 14:13
This is a duplicate of #22180, which was fixed in changeset 9c250f34bfa3 by Raymond Hettinger in branch '3.4'. The fix just removes the bad example, as in my patch. So I suggest that this issue be closed as a duplicate.
msg233422 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2015-01-04 16:18
Date User Action Args
2015-01-04 22:17:50benjamin.petersonsetstatus: open -> closed
2015-01-04 16:18:39asvetlovsetnosy: + asvetlov
messages: + msg233422
resolution: duplicate

superseder: operator.setitem example no longer works in Python 3 due to lazy map
stage: resolved
2015-01-04 14:13:01gdr@garethrees.orgsetmessages: + msg233418
2014-02-15 16:09:55ezio.melottisetnosy: + ezio.melotti
2014-02-14 18:35:50eric.araujosetnosy: + rhettinger, eric.araujo
2014-02-12 12:41:29gdr@garethrees.orgsetfiles: + operator.patch
keywords: + patch
2014-02-12 12:38:32gdr@garethrees.orgsetnosy: +
messages: + msg211088
2014-02-12 10:40:34silent.batcreate