问题:使用列表推导只是副作用是Pythonic吗?

考虑一下我需要它带来副作用的函数,而不是返回值(例如打印到屏幕,更新GUI,打印到文件等)。

def fun_with_side_effects(x):
    ...side effects...
    return y

现在,使用列表推导功能将此功能称为Pythonic

[fun_with_side_effects(x) for x in y if (...conditions...)]

请注意,我不会将列表保存在任何地方

还是我应该这样称呼这个函数:

for x in y:
    if (...conditions...):
        fun_with_side_effects(x)

哪个更好?为什么?

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_with_side_effects(x):
    ...side effects...
    return y

Now, is it Pythonic to use list comprehensions to call this func:

[fun_with_side_effects(x) for x in y if (...conditions...)]

Note that I don’t save the list anywhere

Or should I call this func like this:

for x in y:
    if (...conditions...):
        fun_with_side_effects(x)

Which is better and why?


回答 0

这样做是非常反Python的,任何经验丰富的Pythonista都会为您带来麻烦。中间列表在创建之后就被丢弃了,它可能非常大,因此创建起来很昂贵。

It is very anti-Pythonic to do so, and any seasoned Pythonista will give you hell over it. The intermediate list is thrown away after it is created, and it could potentially be very, very large, and therefore expensive to create.


回答 1

您不应该使用列表理解,因为正如人们所说的那样,这将建立您不需要的大型临时列表。以下两种方法是等效的:

consume(side_effects(x) for x in xs)

for x in xs:
    side_effects(x)

与定义consumeitertools手册页:

def consume(iterator, n=None):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

当然,后者更加清晰易懂。

You shouldn’t use a list comprehension, because as people have said that will build a large temporary list that you don’t need. The following two methods are equivalent:

consume(side_effects(x) for x in xs)

for x in xs:
    side_effects(x)

with the definition of consume from the itertools man page:

def consume(iterator, n=None):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

Of course, the latter is clearer and easier to understand.


回答 2

列表推导用于创建列表。并且,除非您实际创建列表,否则不应使用列表推导。

因此,我将选择第二个选项,即遍历列表,然后在条件适用时调用该函数。

List comprehensions are for creating lists. And unless you are actually creating a list, you should not use list comprehensions.

So I would got for the second option, just iterating over the list and then call the function when the conditions apply.


回答 3

第二更好。

想想需要了解您的代码的人。您可以通过第一个方法轻松获得不良业障:)

您可以使用filter()进入两者之间的中间位置。考虑示例:

y=[1,2,3,4,5,6]
def func(x):
    print "call with %r"%x

for x in filter(lambda x: x>3, y):
    func(x)

Second is better.

Think of the person who would need to understand your code. You can get bad karma easily with the first :)

You could go middle between the two by using filter(). Consider the example:

y=[1,2,3,4,5,6]
def func(x):
    print "call with %r"%x

for x in filter(lambda x: x>3, y):
    func(x)

回答 4

取决于您的目标。

如果尝试对列表中的每个对象执行某些操作,则应采用第二种方法。

如果您尝试从另一个列表生成列表,则可以使用列表理解。

显式胜于隐式。简单胜于复杂。(Python Zen)

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)


回答 5

你可以做

for z in (fun_with_side_effects(x) for x in y if (...conditions...)): pass

但是不是很漂亮

You can do

for z in (fun_with_side_effects(x) for x in y if (...conditions...)): pass

but it’s not very pretty.


回答 6

使用列表理解来产生副作用是丑陋的,非Pythonic的,效率低下的,我不会这样做。我将使用for循环代替,因为for循环表示一种过程样式,其中副作用很重要。

但是,如果您绝对坚持使用列表理解来解决其副作用,则应通过使用生成器表达式来避免效率低下。如果您绝对坚持这种风格,请执行以下两种操作之一:

any(fun_with_side_effects(x) and False for x in y if (...conditions...))

要么:

all(fun_with_side_effects(x) or True for x in y if (...conditions...))

这些是生成器表达式,它们不会生成被抛弃的随机列表。我认为all表格可能会稍微清晰些,尽管我认为两者都令人困惑并且不应该使用。

我认为这很丑陋,实际上我不会在代码中这样做。但是,如果您坚持以这种方式实现循环,那就是我要这样做的方式。

我倾向于认为列表理解和它们的同类应该表明尝试使用至少略带功能性风格的东西。放置带有破坏该假设的副作用的东西将导致人们不得不更仔细地阅读您的代码,我认为这是一件坏事。

Using a list comprehension for its side effects is ugly, non-Pythonic, inefficient, and I wouldn’t do it. I would use a for loop instead, because a for loop signals a procedural style in which side-effects are important.

But, if you absolutely insist on using a list comprehension for its side effects, you should avoid the inefficiency by using a generator expression instead. If you absolutely insist on this style, do one of these two:

any(fun_with_side_effects(x) and False for x in y if (...conditions...))

or:

all(fun_with_side_effects(x) or True for x in y if (...conditions...))

These are generator expressions, and they do not generate a random list that gets tossed out. I think the all form is perhaps slightly more clear, though I think both of them are confusing and shouldn’t be used.

I think this is ugly and I wouldn’t actually do it in code. But if you insist on implementing your loops in this fashion, that’s how I would do it.

I tend to feel that list comprehensions and their ilk should signal an attempt to use something at least faintly resembling a functional style. Putting things with side effects that break that assumption will cause people to have to read your code more carefully, and I think that’s a bad thing.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。