Is it Pythonic to use list comprehensions for just side effects?

后端 未结 7 703
面向向阳花
面向向阳花 2020-11-21 06:23

Think about a function that I\'m calling for its side effects, not return values (like printing to screen, updating GUI, printing to a file, etc.).

def fun_wi         


        
7条回答
  •  时光说笑
    2020-11-21 07:06

    Depends on your goal.

    If you are trying to do some operation on each object in a list, the second approach should be adopted.

    If you are trying to generate a list from another list, you may use list comprehension.

    Explicit is better than implicit. Simple is better than complex. (Python Zen)

提交回复
热议问题