简体   繁体   English

Python正确的代码格式(PEP8)

[英]Python proper code formatting (PEP8)

So I just learned about "List Comprehensions" in python. 所以我刚学会了python中的“List Comprehensions” 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. 其中一些对于单行来说太长了(PEP8),而我正试图找出最好的(最可读的)方法来解决这些问题。

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 但它仍然抱怨[ ]之前的空白,特定的pep8错误是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; 请记住,PEP8旨在与您的最佳判断一起使用; 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. 你的代码看起来有点太类似于类似Lisp的括号语言或类似C语言的大括号语言,因为你将括号和括号放在不同的行上。

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. 在Python中,您只需使用缩进来显示通常使用括号/括号/括号在另一种语言的单独行中显示的内容。 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. 真的,不要过分担心PEP检查器。 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? 哪个工具给你E202? I copy pasted and tried with this pep8 tool and it did not give any error. 我复制粘贴并试用这个pep8工具,它没有给出任何错误。 But I specifically but a whitespace after questions and got the error. 但我特意,但questions后的空白,并得到了错误。

The E202 on the ] says that it is finding a whitespace before that. 关于]的E202说它在此之前找到了一个空白。 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. 在这种情况下,您将questions实例更改为其他类型。

I was also unable to reproduce your PEP8 warning with the code you showed above. 我也无法使用您在上面显示的代码重现您的PEP8警告。 Perhaps you could put your exact code in a pastebin? 也许你可以把你的确切代码放在pastebin中?

The example test cases for PEP8 (if you use the --show-pep8 option) are as follows: PEP8的示例测试用例(如果使用--show-pep8选项)如下:

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. 另外,我实际上并没有使用过Textmate,但是如果你正在进行类似于emacs的flymake模式的动态检查,那么也可能是pep8被调用旧版本的文件,问题可能会出现保存文件时离开。 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 . 至于列表理解本身的格式,您可能想看看这个其他的SO问题以及Google样式指南中的内容 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. 但这真的是关于什么是最易读/可维护的,这取决于你自己的判断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM