python reduce() 有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/181543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:37:23  来源:igfitidea点击:

What is the problem with reduce()?

pythonpython-3.x

提问by jeremy

There seems to be a lot of heated discussion on the net about the changes to the reduce() function in python 3.0 and how it should be removed. I am having a little difficulty understanding why this is the case; I find it quite reasonable to use it in a variety of cases. If the contempt was simply subjective, I cannot imagine that such a large number of people would care about it.

网上似乎有很多关于python 3.0中reduce()函数的变化以及它应该如何删除的激烈讨论。我有点难以理解为什么会这样;我发现在各种情况下使用它是非常合理的。如果只是主观上的鄙视,我想不到这么多人会在意。

What am I missing? What is the problem with reduce()?

我错过了什么?reduce() 有什么问题?

回答by DzinX

As Guido says in his The fate of reduce() in Python 3000post:

正如 Guido在 Python 3000 中的 reduce() 的命运一文中所说:

So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.

所以现在减少()。这实际上是我一直最讨厌的一个,因为除了几个涉及 + 或 * 的例子,几乎每次我看到带有非平凡函数参数的 reduce() 调用时,我都需要拿起笔和纸在我理解 reduce() 应该做什么之前,绘制出实际输入该函数的内容。所以在我看来,reduce() 的适用性几乎仅限于关联运算符,在所有其他情况下,最好明确写出累加循环。

There is an excellent example of a confusing reducein the Functional Programming HOWTOarticle:

reduce函数式编程 HOWTO文章中有一个很好的混淆示例:

Quick, what's the following code doing?

total = reduce(lambda a, b: (0, a[1] + b[1]), items)[1]

You can figure it out, but it takes time to disentangle the expression to figure out what's going on. Using a short nested def statements makes things a little bit better:

def combine (a, b):
    return 0, a[1] + b[1]

total = reduce(combine, items)[1]

But it would be best of all if I had simply used a for loop:

total = 0
for a, b in items:
    total += b

Or the sum() built-in and a generator expression:

total = sum(b for a,b in items)

Many uses of reduce() are clearer when written as for loops.

快点,下面的代码在做什么?

total = reduce(lambda a, b: (0, a[1] + b[1]), items)[1]

你可以弄清楚,但需要时间来理清表达式以弄清楚发生了什么。使用简短的嵌套 def 语句会使事情变得更好:

def combine (a, b):
    return 0, a[1] + b[1]

total = reduce(combine, items)[1]

但如果我只是使用 for 循环,那将是最好的:

total = 0
for a, b in items:
    total += b

或者 sum() 内置和生成器表达式:

total = sum(b for a,b in items)

当编写为 for 循环时,reduce() 的许多用途会更加清晰。

回答by John Millikin

reduce()is not being removed -- it's simply being moved into the functoolsmodule. Guido's reasoning is that except for trivial cases like summation, code written using reduce()is usually clearer when written as an accumulation loop.

reduce()没有被移除——它只是被移动到functools模块中。Guido 的推理是,除了像求和这样的琐碎情况外,用 using 编写的代码reduce()在编写为累加循环时通常会更清晰。

回答by Eli Bendersky

People worry it encourages an obfuscated style of programming, doing something that can be achieved with clearer methods.

人们担心它会鼓励一种混淆的编程风格,做一些可以用更清晰的方法来实现的东西。

I'm not against reduce myself, I also find it a useful tool sometimes.

我不反对减少自己,有时我也发现它是一个有用的工具。

回答by Terminus

The primary reason of reduce's existence is to avoid writing explicit for loops with accumulators. Even though python has some facilities to support the functional style, it is not encouraged. If you like the 'real' and not 'pythonic' functional style - use a modern Lisp (Clojure?) or Haskel instead.

reduce 存在的主要原因是避免使用累加器编写显式 for 循环。尽管 python 有一些工具来支持函数式风格,但不鼓励这样做。如果你喜欢“真实的”而不是“pythonic”的函数式风格——改用现代 Lisp(Clojure?)或 Haskel。