[issue22180] operator.setitem example no longer works in Python 3 due to lazy map

New submission from Michael Williamson: The Python docs for the operator module include an example using map and setitem to "Build a dictionary that maps the ordinals from 0 to 255 to their character equivalents.": d = {} keys = range(256) vals = map(chr, keys) map(operator.setitem, [d]*len(keys), keys, vals) Since map is lazy since Python 3, the dictionary d is never actually changed in this example. I'm not entirely sure what the idiomatic way to fix the example is since it strikes me as being fairly unidiomatic to begin with, but the simplest would be to call list on the result of map to force evaluation (patch attached). ---------- assignee: docs@python components: Documentation files: doc-operator-example.patch keywords: patch messages: 225141 nosy: docs@python, mwilliamson priority: normal severity: normal status: open title: operator.setitem example no longer works in Python 3 due to lazy map versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5 Added file: http://bugs.python.org/file36333/doc-operator-example.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22180> _______________________________________

R. David Murray added the comment: Heh. There was a discussion in issue 22106 about valid examples for using 'pass'. This case is analogous to the one I came up with. for x in map(...): pass that avoids building a list. Not that any of it is idiomatic, as you say. ---------- nosy: +r.david.murray versions: -Python 3.1, Python 3.2, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22180> _______________________________________

Antoine Pitrou added the comment: The whole example is bad and should be removed or replace with something else: 1. Using map() is an anti-pattern here, since the function results are not used. 2. To "build a dictionary that maps the ordinals from 0 to 255 to their character equivalents", modern Python code should use a dict comprehension and avoid operator.setitem() entirely. ---------- nosy: +pitrou, rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22180> _______________________________________

Raymond Hettinger added the comment:
The whole example is bad and should be removed or replace with something else:
I'm not sure there are ANY examples of operator.setitem that couldn't be done a better way without it. How about we remove it and leave the examples for things that people ought to be doing. ---------- assignee: docs@python -> rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22180> _______________________________________

Antoine Pitrou added the comment: Le 10/08/2014 13:20, Raymond Hettinger a écrit :
Raymond Hettinger added the comment:
The whole example is bad and should be removed or replace with something else:
I'm not sure there are ANY examples of operator.setitem that couldn't
be done a better way without it. How about we remove it and leave the examples for things that people ought to be doing. Agreed. operator.setitem() isn't really ever used in my experience. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22180> _______________________________________

Roundup Robot added the comment: New changeset 9c250f34bfa3 by Raymond Hettinger in branch '3.4': Issue #22180: Remove weak example http://hg.python.org/cpython/rev/9c250f34bfa3 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22180> _______________________________________

Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> fixed stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22180> _______________________________________
participants (5)
-
Antoine Pitrou
-
Michael Williamson
-
R. David Murray
-
Raymond Hettinger
-
Roundup Robot