告诉某人“我想将其应用于func
每个元素iterable
的副作用”的首选方式是什么?
# 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]
我喜欢选项 2;标准库中是否有已经等效的函数?