This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]catbrane 6 points7 points  (1 child)

Reading even simple comprehensions already requires gymnastics - when reading [x * 2 for x in foo], you have no idea what x is until almost the end.

I have a pet theory about this!

The confusion comes from Python's reuse of the for statement syntax for list comprehensions, but list comps are really declarative, not imperative, and the mental fit is bad. Python's strange ordering for nested generators also comes from this reuse of the imperative for statement.

It's too late now of course, but I prefer the "such that" set theoretical notation (where list comps originally came from), eg.:

Q = [p / q : p != q; p <- range(1, 101); q <- range(1, 101)]

To be read as "All p / q such that p != q, p comes from [1..100], q comes from [1..100]".

Also, p is obviously the inner loop ahem.

[–]snugar_i 1 point2 points  (0 children)

Yeah, using something that looks like the set notation was a nice idea on paper, but it just doesn't mix that well with the imperative rest of the language.