What is the problem with shorthand ()?

There seems to be a lot of discussion on the net about changes to the reduce () function in python 3.0 and how to remove it. I find it hard to understand why this is so; I consider it quite reasonable to use it in a variety of cases. If contempt was simply subjective, I cannot imagine that so many people would take care of this.

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

+58
python
Oct 08 '08 at 6:27
source share
4 answers

As Guido says in his Destiny reduce () in Python 3000 :

So now let's reduce (). This is actually the one I always hated the most, because, in addition to a few examples involving + or *, almost every time I see a reduce () call with a non-trivial function argument, I need to grab a pen and chart paper, which actually served in this function before I understand what the reduce () method should do. Therefore, in my opinion, the applicability of the reduce () method is largely limited by associative operators, and in all other cases it is better to write the accumulation cycle explicitly.

There is a great example of reduce confusion in the Functional Programming HOWTO :

Quickly, what does the following code do?

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

You can understand this, but it takes time to unravel the expression to figure out what is happening. Using short nested def statements makes things a little better:

 def combine (a, b): return 0, a[1] + b[1] total = reduce(combine, items)[1] 

But it would be best if I just used the for loop:

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

Or the sum () expression and the generator:

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

Many shorthand () functions are more readable when writing as for loops.

+63
Oct 08 '08 at 7:42
source share

reduce() not deleted - it just moves to the functools module. Guido argues that, with the exception of trivial cases, such as summation, code written using reduce() is usually clearer when written as an accumulation loop.

+30
Oct 08 '08 at 7:20
source share

People are worried about encouraging an intricate programming style by doing something that can be achieved using clearer methods.

I am not against myself, I sometimes find it a useful tool.

+8
Oct 08 '08 at 6:53
source share

The main reason for reducing existence is to avoid writing explicit loops for batteries. Although python has some features to support a functional style, this is not recommended. If you like the “real” rather than the “pythonic” functional style, use Lisp (Clojure?) Or Haskel instead.

+1
May 16 '17 at 5:15
source share



All Articles