r/ProgrammerAnimemes • • May 29 '22

Fixed that certain meme about python

Post image
1.1k Upvotes

59 comments sorted by

View all comments

Show parent comment

47

u/[deleted] May 29 '22

[removed] — view removed comment

47

u/newo2001 May 30 '22

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.

27

u/MrMonday11235 May 30 '22

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?

3

u/newo2001 May 30 '22

This is genuinely a really good point.

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.

2

u/MrMonday11235 May 30 '22

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.

→ More replies (4)

10

u/jaber24 May 30 '22

When you don't try to do too much list comprehension is definitely more readable than the alternatives imo

36

u/sexytokeburgerz May 30 '22

As a js dev i’m just really happy that it doesnt have so many fucking brackets

15

u/netflixandbinge May 30 '22

omg trying to find where you've missed a bracket in js. kill me.

27

u/killerrin May 30 '22

Brackets are easy. Just run the formatter and look for where it first breaks. Or use Visual Studio Code and match up the colours of the braces.

5

u/sexytokeburgerz May 30 '22 edited May 30 '22

I do both of those things, and i find the problem immediately anymore, but i still question the necessity of the entire process.

setTimeout ( function pleaseTellMe(why, the, fuck){

let thisShouldExist = why + the + fuck;

clearInterval(pleaseTellMe);

},42069);

2

u/killerrin May 30 '22

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.

6

u/[deleted] May 30 '22

[removed] — view removed comment

8

u/newo2001 May 30 '22

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.

→ More replies (1)

9

u/[deleted] May 30 '22 edited May 30 '22

Lisp ~ Python Lisp

(loop for x in arr 
      if (> x 3)
      collect (1+ x))

edit: I hate Reddit markdown.

3

u/AlarmingAffect0 May 30 '22

Yeth, Mathter, tho do we for thure.

→ More replies (1)

4

u/Mast3r_waf1z May 30 '22

At this point why don't you just write C tho?

5

u/[deleted] May 30 '22

[removed] — view removed comment

2

u/Mast3r_waf1z May 30 '22

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

2

u/[deleted] May 30 '22

[removed] — view removed comment

5

u/Mast3r_waf1z May 30 '22

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)

3

u/[deleted] May 30 '22

[removed] — view removed comment

2

u/Mast3r_waf1z May 30 '22

I just gave you an example of what you could do in C if you really needed them abstracted from your own code but it would only really matter if you write bigger programs, you know, you wouldn't write a whole sum function to use it once

→ More replies (2)

2

u/FierceDeity_ Jun 08 '22

Man that's exactly why I also love Elixir.

1..11 |> Enum.sum |> IO.puts

Putting these operations into a readable order makes it so much nicer to read.

BTW, |> (pipe) literally just takes the return value of an expression and uses it as the first argument to the next function in the pipe

→ More replies (3)