Note: this is a convention question; the code does work. That said, I am polishing up an 8 mo. project and can't figure out the best way to style the following:
def foo(Xs, Ys, image):
products = []
for prod in product(Xs,Ys):
products.append([prod])
items = []
for comb in products:
for item in comb:
items.append(item)
return [[[t[1][0], t[0][0]],
image[t[1][0]:t[1][1],
t[0][0]:t[0][1]]]
for t in list(set(items))]
So I have 2 questions: 1) I know flat is better than nested, but can I return a (not-so) one-line list like this without breaking convention too much? 2) If I do want to return this beast as a pseudo-one-liner, what is the PEP8 convention regarding the spaces and brackets?
Edit: the second loop in the code was for debugging reasons and I completely forgot to take it out. Embarrassing.
It is updated to this:
def foo(Xs, Ys, image):
products = []
for prod in product(Xs,Ys):
products.append(prod)
return [[[t[1][0], t[0][0]],
image[t[1][0]:t[1][1],
t[0][0]:t[0][1]]]
for t in set(products)]
As far as I know, PEP8 doesn't address these long, multi-line list-comprehensions explicitly. Strictly speaking, as long as you keep the line-length down, you are "fine".
But look, I would avoid code like this like a plague. It's fun for messing around playing code-golf, but not for writing readable, maintainable code. That is the whole point of PEP8. The first thing in our tool-box for writing readable, maintainable code is to use functions.
def foo(Xs, Ys, image):
products = []
for prod in product(Xs,Ys):
products.append([prod])
items = []
for comb in products:
for item in comb:
items.append(item)
return [mogrify(t, item) for t in list(set(items))]
def mogrify(t, item):
return [[t[1][0], t[0][0]], image[t[1][0]:t[1][1], t[0][0]:t[0][1]]]