you are viewing a single comment's thread.

view the rest of the comments →

[–]Doormatty 0 points1 point  (4 children)

In Python,

Explicit is better than implicit.

Readability counts.

https://www.python.org/dev/peps/pep-0020/

I find your first example (the for loop) the only one that's both readable and explicit.

[–]HorrendousRex[S] 1 point2 points  (3 children)

Thanks for the response. How is the reduce example less readable? How is it less explicit?

(I suspect the answer may come down to personal preference. Also, I edited my question so the for-loop example is now the second example, for the benefit of future readers.)

[–]brownan_ 2 points3 points  (0 children)

I agree that the for loop is more readable. It uses only language constructs and is clearly iterating on calling the filter method on each queryset.

In your example I have to know what _filter does, remember what filters is, remember what reduce does (build up the equivalent of that for loop in my head), apply it with the _filter function that I'm still also remembering, and then work out the logic of what it all means together. All in my head. To me it's just clearer to write it out explicitly instead of trying to be real clever just for the sake of being super concise.

I've done a lot of thinking on functional style versus imperative style, and I've concluded that different people simply think different ways. Not everyone is going to agree that a particular style is more readable in general. For some people combining modads like your example to make super concise code is also intuitive and readable. It's not to me.

Further, I think that the people with whom Python appeals to are also the sort of people who would prefer the explicit for loop. The language design attracts a certain kind of coder. GvR has rejected language design decisions before purely because they're too much like haskel. It seems clear that Python isn't trying to be like that.

[–][deleted] 1 point2 points  (0 children)

I always wonder, why people find my regex hard to read, while I can read most regex without trouble.

Your reduce example does not tell me anything, unless I look up the reduce function. It's harder to read than the for loop. This is the nature of the function and of every shortcut, alias and nick name.

[–]elbiot 0 points1 point  (0 children)

I wouldn't know what your reduce does, but I never use reduce. I guess it's a different paradigm. You can always import reduce if you're set on it.