Changes between Version 26 and Version 27 of DeveloperGuidelines/Py_2_3
- Timestamp:
- 06/28/19 08:31:29 (6 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DeveloperGuidelines/Py_2_3
v26 v27 131 131 === Map and Zip return generators === 132 132 133 In Python-3, the {{{map()}}} and {{{zip()}}} functions return generator objects rather than lists. Where lists are required, the return value must be converted explicitly using the list constructor. Where possible, we prefer list comprehensions. 133 In Python-3, the {{{map()}}} and {{{zip()}}} functions return generator objects rather than lists. This is fine when we want to iterate over the result, especially when there is a chance to break out of the loop early. 134 135 But where lists are required, the return value must be converted explicitly using the list constructor. 136 134 137 {{{#!python 135 138 # This is fine: … … 139 142 # This is better: 140 143 result = list(map(func, values)) 141 # This could be even better (though not for multiple iterables):144 # This could be even better: 142 145 result = [func(v) for v in values] 143 146 }}} 147 148 '''NB''' For building a list from a single iterable argument, we prefer list comprehensions over {{{map()}}} for readability and speed.