Message211088
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> |
|
Date |
User |
Action |
Args |
2014-02-12 12:38:32 | gdr@garethrees.org | set | recipients:
+ gdr@garethrees.org, docs@python, silent.bat |
2014-02-12 12:38:32 | gdr@garethrees.org | set | messageid: <1392208712.4.0.373903895258.issue20606@psf.upfronthosting.co.za> |
2014-02-12 12:38:32 | gdr@garethrees.org | link | issue20606 messages |
2014-02-12 12:38:31 | gdr@garethrees.org | create | |
|