Efekty uboczne na mapie Pythona (blok python „do”)
Jaki jest preferowany sposób mówienia komuś „Chcę złożyć wniosekfunc
do każdego elementuiterable
za skutki uboczne. ”
# Option 1... clear, but two lines.
for element in iterable:
func(element)
# Option 2... even more lines, but could be clearer.
def walk_for_side_effects(iterable):
for element in iterable:
pass
walk_for_side_effects(map(func, iterable)) # Assuming Python3's map.
# Option 3... builds up a list, but this how I see everyone doing it.
[func(element) for element in iterable]
Podoba mi się opcja 2; czy w standardowej bibliotece istnieje funkcja, która jest już odpowiednikiem?