This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Operator Documentation Example doesn't work
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.3
process
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, gdr@garethrees.org, rhettinger, silent.bat
Priority: normal Keywords: patch

Created on 2014-02-12 10:40 by silent.bat, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
operator.patch gdr@garethrees.org, 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
http://docs.python.org/3.3/library/operator.html#operator.__setitem__

the following example doesn't work.
msg211088 - (view) Author: Gareth Rees (gdr@garethrees.org) * (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.

<http://code.ohloh.net/search?s=%22operator.setitem%22&pp=0&fl=Python&mp=1&ml=1&me=1&md=1&ff=1&filterChecked=true>
msg233418 - (view) Author: Gareth Rees (gdr@garethrees.org) * (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
Closed
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64805
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: + gdr@garethrees.org
messages: + msg211088
2014-02-12 10:40:34silent.batcreate