I don't get the obsession over "pythonic syntax" I just don't understand how: [x+1 for x in arr if x > 3]
Is more readable than the pattern used by almost every other language like: "arr.filter(x => x > 3).map(x => x+1)" or "arr.Where(x => x > 3 ).Select(x => x+1)" or something similar.
I don't know that it's necessarily "more readable" for people who do this for a living, but the code is much more of a natural extension of simpler/earlier language features -- even if you only knew basic loops and conditionals and had never seen/heard of a list comprehension before seeing that statement, you'd intuitively understand exactly what the end result is.
Compare that to arr.filter(x => x>3).map(x => x+1), where you'd need to be familiar with both lambda functions and how map works, or arr.Where(x => x > 3 ).Select(x => x+1), which ALSO requires you to know a non-intuitive definition of "Select" (i.e. the SQL SELECT rather than the common English "select", which is closer to "filter"), and, looking at things from the perspective of someone learning the language, it's quite easy to see why the Python syntax is preferred for people that value that.
And since you can have basically that exact syntax in Python as well, with map(lambda x: x+1, filter(lambda x: x>3, arr)), it's nice to have the option, no?
I agree that pythonic syntax is clearer to somebody without much experience in other languages. With this experience comes understanding of these fundemental concepts like method chaining and lamda functions, which, atleast for me allows me to reason as to why this works without having it be this "magical language feature".
Not saying that list comprehension is bad as you stated there is the option with map and filter which can act as a substitute. Although being mathematically logical by utilising function composition, I do not like having to read the expression backwards (inside out).
That is why I think the chaining or even better, forward piping solution in functional languages, is easier to reason about.
Atleast list comprehension beats the Java stream api by a long shot.
Oh, I'm 100% in agreement that piping a la Erlang/Elixir is the best.
I'm also 100% in agreement that the Java stream API is straight trash.
As far as the function composition being read inside out... I mean, you get used to it with most of the "mainstream" programming languages, so while it's definitely an annoyance, it's also an inherited (heh) annoyance moreso than a deliberate decision to have it be that way. Blame Algol for that, I guess.
Its literally the same issue/resolution process with Python, just replace the word Brackets with Indentation. Only with the difference that the formatter can't infer a broken indentation error from a missing end bracket.
It reads backwards, just like the American date representation. It is trying to be logical by following english grammatical structure which is not the order that the statements are executed in.
[<map> for <value> in <enumerable> if <filter>]
Looking at the process as a pipeline, "enumerable -> filter -> map" just makes a lot more sense.
It's syntax isn't much different from so many other languages though? Most notably just making datatypes optional and replacing curly brackets with indents
Okay but you're just talking about built-in functions that aren't necessarily python specific, if you really needed a sum and range function to be abstracted from your code why not just write your own math library and import that? (I don't wanna Google a ton about libraries in other languages but it probably already exists)
View all comments
172
u/[deleted] May 29 '22
[removed] — view removed comment