If I see this kind of "cute" code in code reviews, there is some serious scolding to be done.
If I see this kind of "cute" code in code reviews, there is some serious scolding to be done.
planets_set = {
planet for episode in episodes.values()
for planet in episode['planets']
}
is less maintainable in a Python shop than say... planets = set()
for episode in episodes.values():
planets.update(episode['planets'])
Although the latter will likely make perfect sense to most non-python developers. The former is faster and has a smaller memory footprint and might be preferred when dealing with a larger or more irregular data sets.The former also has the advantage of not leaking the "episode" variable into the function/method scope, which could introduce a subtle bug if that variable gets conditionally reused. So while it's harder to understand for a less-experienced python developer, the set comprehension solution is inherently safer due to python's design.
coordinates = (star.x, star.y, star.z for star in star_map)Nested list comprehensions with multiple filtering if statements? Scald away.
But the simpler examples there are perfectly readable, understandable and maintainable if you understand list comprehensions. And if you need to alter it to the point where it needs to be a little more verbose in order to convey what you're doing, then sure, you can break it out into some other structure. But just because the complexity might increase later on doesn't mean that you shouldn't use a list comprehension.
And for those few lines of code you've traded the ability for new programmers to understand it easily. To me that's a net loss.
set/dict/generator comprehensions do not.
Example:
for foo in bar:
baz(foo)
print foo # Will actually print something
Whereas whatever = {baz(foo) for foo in bar}
print foo # NameError: name 'foo' is not defined
does not put foo into the outer scope.list comprehensions in python 2 (but not 3) will leak variables however.
good_jedis = [jedi for jedi in universe if jedi.alignment == "good"]
vs. good_jedis = []
for jedi in universe:
if jedi.alignment == "good":
good_jedis.append(jedi)
And then when you get on stuff like dictionary comprehensions and generator expressions, it starts becoming an amazing tool that is able to accomplish things that are straight-up cumbersome with old loops.There's a computer science course out there that a friend was telling me about, that begins by showing students how to "map" a function over an iterable, way way before a "for loop" ever shows up. I think this is the way it should be done, "for loops" as a three-line-structure only seem natural to people who grew up programming for loops.
List comprehensions (and inline generators, which you get by using parenthesis) make it trivial to handle fairly complex sequence processing, and are one of the reasons Python gets a lot of love from the FP community.
Sure, it's not really an FP language, but these give it an almost LISPy feel.
Regardless,it would be less Pythonic to build a loop and iterate.
Might be easier for complete novices, but I don't suppose people are just hiring novices these days, right?
(edit: Mobile-sponsored typo)
But it's not relevant. What relevant is, do they create a readability problem for a python developer who spent significant time with them? I honestly don't know.
I am rather fond of dictionary comprehensions as mentioned in the article:
colors = [jedi['lightsaber_color'] for jedi in jedis]
frequencies = {color: colors.count(color) for color in set(colors)}
print(frequencies)
# {'green': 6, 'red': 5, 'blue': 6}Languages that support it directly on a list are much more intuitive in my head: list.filter().map()...
Alternatively, pull in syntax like elixir (and other languages) have to make chaining a breeze:
list |> map() |> filter()