3 ms·
Frankly, comprehensions are one of Python's worst features. Some properly implemented functional syntax would be both more readable and concise. Plus double-nes
by c3534l 11y ago
Frankly, comprehensions are one of Python's worst features. Some properly implemented functional syntax would be both more readable and concise. Plus double-nested (I'm not sure what you'd call it) comrpehensions seem to be backwards. Rather than [ingredient for ingredient in recipe for recipe in cookbook] it's [ingredient for recipe in cookbook for ingredient in recipe] which makes it sound as if there's only going len(recipe) number of ingredients in your list. Plus it's so verbose and horizontal, half the time if you want descriptive names you're gonna write something so verbose it doesn't even make it that much more readable.
- oinksoft 11y agoHaskell, Erlang, and Clojure all have sane lambdas (assuming that's what you mean by "properly implemented functional syntax"), and list comprehensions are useful and idiomatic in those languages. Agreed about sequence names feeling out-of-order in nested comprehensions in Python.
- darkerside 11y agoFrom the Zen of Python: > There should be one-- and preferably only one --obvious way to do it. If you have something written as a simple for loop, there's a real danger that you're moving in the wrong direction by translating it into a list comprehension.
- iamsohungry 11y ago> Plus double-nested (I'm not sure what you'd call it) comrpehensions seem to be backwards. Why are you even doing this? This isn't an acceptable way to write code. > Plus it's so verbose and horizontal, half the time if you want descriptive names you're gonna write something so verbose it doesn't even make it that much more readable. If your list comprehension is more than 80 characters wide, it's not because list comprehensions are bad, it's because you did something too complicated. Pull it into a function and name it so people know what the hell you're doing.
- has2k1 11y agoWhen reading code I find comprehensions easier to comprehend than combinations of map, filter and reduce. That is considering that I played around with some Common-Lisp before getting into Python. The parts in the comprehension [*wanted* *iteration(s?)* *condition?*] compares well to *iterations(s?)* *condition?* *append wanted* for normal looping behaviour without changing order of the iterations. One of my favourite comprehensions is of the sort [item for item in items in for i in (0, 1)] of course this can be the same as list(items) * 2 but the comprehension is more versatile [item for item in items in for i in (0, 1) if items[0] != 'a'] For most once the mental fog clears comprehensions become natural very quickly as they are not that far removed from normal loops.
- deleted 11y ago[deleted]