News for hackers. Stuff that matters.
Re:Python is not a great programming language by mattlondon on Saturday October 12, 2019 @04:53PM (#21236173)
I personally find list comprehensions in python pretty horrible.

They seem to exist only to do lots of stuff in one single line of code. You end up with totally impenetrable unreadable perl-esq garbage write-once-read-never code that is too clever for its own good. And people say python is easy to learn and good for beginners...!

A better approach would be something like Java Streams/.net Lync/RxX pattern IMO. Explicit, clear, no magic, logical.

Re:Python is not a great programming language by novok on Saturday October 12, 2019 @05:02PM (#21236271)
You can decompose any list comprehension into it's equivalent for loop fairly easily. I don't see what is so magic about them other than the language having a special compiler level optimization for them?

g = [i for i in list if x == 3] -> g = []; for i in list: if x == 3: g.append(i);

Re:Python is not a great programming language by mplanchard on Saturday October 12, 2019 @05:48PM (#21236598)
I see people frequently say that list comprehensions are special-cased and highly optimized by the compiler, but I did a few experiments recently with `timeit` and found that using `map()` is on the order of 1.5x faster in all the cases I tested. I think that either winds up being faster than a manual for loop, though
Re:Python is not a great programming language by ledauphin on Saturday October 12, 2019 @04:58PM (#21236229)
I personally find them extremely readable, and a lot clearer than for loops about the lack of side effects.
Re:Python is not a great programming language by BenFrantzDale on Saturday October 12, 2019 @05:00PM (#21236244)
I disagree. It’s basically set-builder notation, which probably predates any programming language. It’s a concise way to express filtering.
Re:Python is not a great programming language by genidoi on Saturday October 12, 2019 @05:03PM (#21236293)
dic = {k: v for k, v in dic.items() if k in other_dic and v == "bar"}

How could that be improved? That's 3-4 LOC minimum in any other language

My main grip is python's ternary operators, since the True value is evaluated before the condition, if you are doing ternaries on things that might throw exceptions the False value has to come first

value = 0 if key not in dic else dic[key] * 5

rather than (throws indexerror if key isn't in dic)

value = dic[key] * 5 if key in dic else 0

Re:Python is not a great programming language by grzm on Saturday October 12, 2019 @05:20PM (#21236409)

    dic = {k: v for k, v in dic.items() if k in other_dic and v == "bar"}
> "How could that be improved? That's 3-4 LOC minimum in any other language"

If I'm understanding the comprehension correctly,

    (into {} (filter (fn [[k v]] (and (get other-dic k) (= v "bar"))) dic))

Though, for readability, I'd likely write it as:

  (->> dic
       (filter (fn [[k v]]
                 (and (get other-dic k)
                      (= v "bar"))))
       (into {}))
Legibility is in the eye of the beholder.
Re:Python is not a great programming language by genidoi on Saturday October 12, 2019 @05:23PM (#21236426)
Each to their own, but both the snippets you posted crossed my threshold for headache inducing parentheses tracking
Re:Python is not a great programming language by grzm on Saturday October 12, 2019 @05:24PM (#21236434)
Similarly, my parsing of special-case syntax in the Python.
Re:Python is not a great programming language by genidoi on Saturday October 12, 2019 @05:26PM (#21236450)
What special-case syntax? The only brazenly pythonic thing in there was the (iterative) tuple unpacking, as in

(for) k, v in dic.items()

which is just

k, v = (<key>, <value>) for every key value pair in the dictionary

Re:Python is not a great programming language by grzm on Saturday October 12, 2019 @05:32PM (#21236496)
There are a number of precedence rules you need to keep track of to parse the list comprehension. There are two different syntaxes that do the same thing. Arguably you need to do the same if you don't already know how the threading macro in the my second example works.

I posted due to your claim regarding all other languages necessitating increased verbosity. I should have left it lie, as I didn't intend to promote a language war, just to post a counter example. My apologies.

Re:Python is not a great programming language by genidoi on Saturday October 12, 2019 @05:48PM (#21236594)
No language war intended, just curious how something I see as a wheel could be improved.
Re:Python is not a great programming language by pryffwyd on Saturday October 12, 2019 @05:34PM (#21236520)
It appears textually first but the evaluation order is the same as the classic ?: ternary. ```[][0] if False else 'foo'``` will not raise an IndexError.
Re:Python is not a great programming language by 1wd on Saturday October 12, 2019 @05:45PM (#21236583)
I like comprehensions, but think this style has some advantages (and some disadvantages):

dic = dic.where((k, v) => k in other_dic and v == "bar").todict()

Re:Python is not a great programming language by fiedzia on Saturday October 12, 2019 @09:10PM (#21237550)
> dic = {k: v for k, v in dic.items() if k in other_dic and v == "bar"}

dic.iter().filter(|k, v| other_dic.contains(k) && v == "bar").collect();

it's one line, though I'd format it as 3 for readability (list comprehension is hard to read and functional style composes better.

Re:Python is not a great programming language by FpUser on Saturday October 12, 2019 @05:00PM (#21236254)
" impenetrable unreadable perl-esq garbage write-once-read-never code " - I had about the same impression when I first saw it. I think ternary operator is as far as I am willing to go ;)
Re:Python is not a great programming language by zzyzxd on Sunday October 13, 2019 @02:50AM (#21238817)
list comprehensions can be elegant and actually improve readability. However, I do agree that it can be easily misused. I have seen many junior Python programmers writing super long, complicated comprehensions that hurt my brain. They think it is pretty cool just because their solutions are one-liners.
Re:Python is not a great programming language by mythrwy on Saturday October 12, 2019 @05:05PM (#21236308)
List comprehensions are one of those abuse subject features (like most things) where light simple use is fantastic but some people just take it too far into unreadable nastiness.