So I just learned about “List Comprehensions” in python. some of these are getting too long for a single line (PEP8) and I’m trying to figure out the best (most readable) way to break these out.
I’ve come up with this
questions = [
(
q,
q.vote_set.filter(choice__exact='Y'),
q.vote_set.filter(choice__exact='N'),
request.session.get(str(q.id))
)
for q in questions
]
but it still complains about whitespace before the ], the specific pep8 error is E202
this is in an indented block.
I would probably do it like this:
Keep in mind that PEP8 is intended to be used along with your best judgement; they aren’t intended to be followed absolutely in all circumstances. They also aren’t structured to always make sense when multiple rules conflict.
It’s OK to intentionally break the rules once in a while; checkers like that are just intended to make sure you don’t break them accidentally.
Edit: Moving my comment into my answer.
Your code looks a little bit too much like a Lisp-like parenthesis language or a C-like curly-braces language because of you putting brackets and parenthesis on separate lines.
In Python, you just use indentation to show what you would normally show with a bracket / parenthesis / brace on a separate line in another language. If you take your code and make that change, it’s identical to my version.
Really though, don’t worry too much about the PEP checker. If you really like the extra whitespace you get from putting the parenthesis and brackets on separate lines, then do it. It doesn’t make it “bad code” nor does it decrease the readability.