msg141470 - (view) |
Author: Jason R. Coombs (jaraco) *  |
Date: 2011-07-31 15:28 |
In `whatsnew/3.0.html`, there is little said about the map builtin: map() and filter() return iterators. If you really need a list, a quick fix is e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful). This suggests that all one must do to port to Python 3 is wrap map in list, and in fact this is what the 2to3 fixers do, when in fact, map is semantically different in how it handles arguments of different lengths. Consider the following. def to_tuple(*args): return args print(list(map(to_tuple, [1,2,3], [4,5,6,7]))) On Python 2, this code outputs: [(1, 4), (2, 5), (3, 6), (None, 7)] On Python 3, this code outputs: [(1, 4), (2, 5), (3, 6)] I suggest three fixes (in order of importance): 1) Document the difference in whatsnew/3.0.html. 2) Describe how one should port code of this usage to 3.0. 3) Incorporate the porting in the 2to3 fixer (if possible). If no one objects, I'll begin on (1). Does anyone have any suggestions for a clean approach to (2)? |
|
|
msg141471 - (view) |
Author: Jason R. Coombs (jaraco) *  |
Date: 2011-07-31 16:02 |
I believe the correct solution to (2) is to use itertools.zip_longest. So to port to Python 3, the example would use: print(list(map(to_tuple, itertools.zip_longest([1,2,3], [4,5,6,7])))) |
|
|
msg141537 - (view) |
Author: Jason R. Coombs (jaraco) *  |
Date: 2011-08-01 22:04 |
I've created a patch to address (1) and (2). Is there any value in also including this in the 2to3 fixer? I can see that it's a simple translation, but adds complexity to the converted code. I'd be content to go with just a documentation patch. I'll defer to Benjamin or his delegate on whether or not to update 2to3. |
|
|
msg141668 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2011-08-05 16:29 |
(HTTPS repos are not supported) |
|
|
msg141669 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2011-08-05 16:30 |
Can you provide a public URI? |
|
|
msg141670 - (view) |
Author: Jason R. Coombs (jaraco) *  |
Date: 2011-08-05 16:35 |
I don't know how that repo got to be private; I created it just like every other public repo I have, and I thought I was only allowed one private repo (this was my second). In any case, I updated the setting, and now it appears to be accessible via the bug tracker. |
|
|
msg141671 - (view) |
Author: Jason R. Coombs (jaraco) *  |
Date: 2011-08-05 16:39 |
I'm not sure how the bugtracker patch mechanism works, but the patch it produced included a lot of changes that I didn't make (changesets already committed to the master repo). |
|
|
msg141718 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2011-08-06 14:53 |
I have reported the bugs to the metatracker. In the meantime, could you manually upload a diff? |
|
|
msg141720 - (view) |
Author: Jason R. Coombs (jaraco) *  |
Date: 2011-08-06 16:18 |
I'm attaching the diff at https://bitbucket.org/jaraco/cpython-issue12666/changeset/bc362109eed8 |
|
|
msg141778 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2011-08-08 14:23 |
The content of the patch is very helpful, but I question its location: I’m not sure people will find this nugget in the 3.2+ version of the What’s New in Python 3.0 document (sorry for not bringing that up sooner). Maybe you could update Doc/library/functions.rst instead, and/or Doc/howto/pyporting.rst? |
|
|
msg148802 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2011-12-03 14:00 |
New changeset 3b505df38fd8 by Jason R. Coombs in branch '3.2': Issue #12666: Clarifying changes in map for Python 3 http://hg.python.org/cpython/rev/3b505df38fd8 New changeset 0e2812b16f5f by Jason R. Coombs in branch '3.2': Issue #12666: Added section about map changes. http://hg.python.org/cpython/rev/0e2812b16f5f New changeset 51af35bd46f7 by Jason R. Coombs in branch 'default': Merge fix for Issue #12666 from 3.2 http://hg.python.org/cpython/rev/51af35bd46f7 |
|
|