3 ms·
Another effective technique for making readable, maintainable code is to break the list comprehension out into a generator (as a closure): This example…: a
by gorgoiler 4y ago
Another effective technique for making readable, maintainable code is to break the list comprehension out into a generator (as a closure):
This example…:
a = …
vs = [
v(x)
for b in a.f()
for x in b
if x and c1(x)
and not c2(x)
]
…becomes:
a = …
def each_v():
for b in a.f():
for x in b:
if not c1(x): # comment
continue
if c2(x): # comment
continue
if x:
yield v(x)
vs = set(each_v())
Line orienting each conditional makes commenting much easier. The collection used for vs is clear. The formatting is less susceptible to being churned up by black. You get to give the thing a name, which helps.