|
|
|
|
|
by mathisfun123
1020 days ago
|
|
the lady doth protest a bit too much me thinks > Recursive list comprehensions with their lack of parentheses are unreadable to me to this day (e.g.: `[item for sublist in list_of_lists for item in sublist]`) 1. there's no recursion here 2. it's literally just a double nested loop flattened (i.e. exactly matching the semantics) to be on the same line instead of indented and on two lines: [item for sublist in list_of_lists for item in sublist]
is the same as for sublist in list_of_lists:
for item in sublist:
item
like why would you need parens when you know the for starts the next nested loop. |
|
Well that's the thing, I don't know the "for" starts the next nested loop because there's no indicator for that, and aside from that it's harder to see it at a glance. Nested loops have a colon, line break and deeper indentation inbetween, here you get nothing.
To me it feels like it breaks Python's own rules: the language omits curly braces for scopes but ensures readability with colon & indentation. But here the chained list comprehension nests two loops without any visible scope boundaries.
Perhaps it's just me and others can read it just fine, but that's my two cents on it.