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.setitem example no longer works in Python 3 due to lazy map
Type: Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, mwilliamson, pitrou, python-dev, r.david.murray, rhettinger
Priority: normal Keywords: patch

Created on 2014-08-10 14:18 by mwilliamson, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
doc-operator-example.patch mwilliamson, 2014-08-10 14:18 review
Messages (6)
msg225141 - (view) Author: Michael Williamson (mwilliamson) Date: 2014-08-10 14:18
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).
msg225142 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-08-10 15:13
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.
msg225145 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-10 17:10
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.
msg225146 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-08-10 17:20
> 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.
msg225147 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-10 17:23
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.
msg225149 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-08-10 17:33
New changeset 9c250f34bfa3 by Raymond Hettinger in branch '3.4':
Issue #22180:  Remove weak example
http://hg.python.org/cpython/rev/9c250f34bfa3
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66376
2015-01-04 16:18:39asvetlovlinkissue20606 superseder
2014-08-10 17:35:07rhettingersetstatus: open -> closed
resolution: fixed
stage: resolved
2014-08-10 17:33:35python-devsetnosy: + python-dev
messages: + msg225149
2014-08-10 17:23:41pitrousetmessages: + msg225147
2014-08-10 17:20:20rhettingersetassignee: docs@python -> rhettinger
messages: + msg225146
2014-08-10 17:10:03pitrousetnosy: + rhettinger, pitrou
messages: + msg225145
2014-08-10 15:13:11r.david.murraysetnosy: + r.david.murray

messages: + msg225142
versions: - Python 3.1, Python 3.2, Python 3.3
2014-08-10 14:18:24mwilliamsoncreate