开发者

Python proper code formatting (PEP8)

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 reada开发者_StackOverflowble) 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:

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]

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.


Depends upon to the tool, I guess. Which tool is giving you E202? I copy pasted and tried with this pep8 tool and it did not give any error. But I specifically but a whitespace after questions and got the error.

The E202 on the ] says that it is finding a whitespace before that. Make sure that you don't have that in the code. Try closing ] soon after questions.


Consider writing your statement using a generator expression.

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)

Additionally, not that its "wrong", but in general I don't recommend redefining declared variables cause it may cause confusion in the code. In this case you are changing the questions instance to another type.


I was also unable to reproduce your PEP8 warning with the code you showed above. Perhaps you could put your exact code in a pastebin?

The example test cases for PEP8 (if you use the --show-pep8 option) are as follows:

Avoid extraneous whitespace in the following situations:

- Immediately inside parentheses, brackets or braces.

- Immediately before a comma, semicolon, or colon.

Okay: spam(ham[1], {eggs: 2})
E201: spam( ham[1], {eggs: 2})
E201: spam(ham[ 1], {eggs: 2})
E201: spam(ham[1], { eggs: 2})
E202: spam(ham[1], {eggs: 2} )
E202: spam(ham[1 ], {eggs: 2})
E202: spam(ham[1], {eggs: 2 })

E203: if x == 4: print x, y; x, y = y , x
E203: if x == 4: print x, y ; x, y = y, x
E203: if x == 4 : print x, y; x, y = y, x

Also, I haven't actually used Textmate, but if you're doing on the fly checking similar to emacs' flymake mode, then it could also be that pep8 is getting called on an old version of the file, and the issue may go away when you save the file. We may need more information to debug further.

As for the formatting of the list comprehension itself, you may want to take a look at this other SO question as well as the take from the Google style guide. I personally have no problem with the way you did it. I suppose you could also do something like

def _question_tuple(q):
    return (
        q,
        q.vote_set.filter(choice__exact='Y'),
        q.vote_set.filter(choice__exact='N'),
        request.session.get(str(q.id))
    )

question_tups = [_question_tuple(q) for q in questions]

but it's really about what will be the most readable/maintainable, and that's up to your own judgment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜