4 ms·
Nesting them I feel is almost always bad blarg = [ foo(x) for x in y if x > 2] I think is pretty decent. Helps if they are calling named functions, so it's m
by mpdehaan2 11y ago
Nesting them I feel is almost always bad
blarg = [ foo(x) for x in y if x > 2]
I think is pretty decent. Helps if they are calling named functions, so it's more self documenting, depending on complexity.
Dict comprehensions can be a little brain-warping too :)
- kbenson 11y agoYeah, the Perl equivalent of that is: my @blarg = map { foo($_) } grep { $_ > 2 } @y; A bit more verbose, but if you know Perl, obvious (and the same building blocks allow for more powerful constructs[1], since they really are functional code blocks). You can argue the Python version is clearer, but placement of the conditional always throws me off. I like to think of my transforms as a pipeline. 1: http://en.wikipedia.org/wiki/Schwartzian_transform http://en.wikipedia.org/wiki/Schwartzian_transform