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

Michael Williamson report at bugs.python.org
Sun Aug 10 16:18:24 CEST 2014


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 at python
components: Documentation
files: doc-operator-example.patch
keywords: patch
messages: 225141
nosy: docs at 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 at bugs.python.org>
<http://bugs.python.org/issue22180>
_______________________________________


More information about the New-bugs-announce mailing list