Changes between Version 26 and Version 27 of DeveloperGuidelines/Py_2_3


Ignore:
Timestamp:
06/28/19 08:31:29 (5 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Py_2_3

    v26 v27  
    131131=== Map and Zip return generators ===
    132132
    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.
     133In 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
     135But where lists are required, the return value must be converted explicitly using the list constructor.
     136
    134137{{{#!python
    135138# This is fine:
     
    139142# This is better:
    140143result = list(map(func, values))
    141 # This could be even better (though not for multiple iterables):
     144# This could be even better:
    142145result = [func(v) for v in values]
    143146}}}
     147
     148'''NB''' For building a list from a single iterable argument, we prefer list comprehensions over {{{map()}}} for readability and speed.