“空行”中的Python缩进 [英] Python indentation in "empty lines"

查看:99
本文介绍了“空行”中的Python缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是首选(。表示空白)?

Which is preferred ("." indicating whitespace)?

A)

def foo():
    x = 1
    y = 2
....
    if True:
        bar()

B)

def foo():
    x = 1
    y = 2

    if True:
        bar()

我的直觉是B(这也是vim为我做的),但我看到人们一直使用A)。是因为大多数的编辑都是破碎的。

My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are broken?

推荐答案

PEP 8 似乎不清楚这个问题,虽然关于空白行的说法可以解释为赞成B. PEP 8风格检查器(pep8.py)偏好B,并警告如果你使用A;然而,这两种变化都是合法的。我自己的观点是,因为Python将成功地解释代码在任何情况下,这并不重要,并试图执行它将是一个很大的工作,非常少的收益。我想如果你非常坚定地支持一个或另一个你可以自动转换到另一个。试图手动修复所有这些线,将是一个巨大的任务,真的不值得努力,IMHO。

The PEP 8 does not seem to be clear on this issue, although the statements about "blank lines" could be interpreted in favor of B. The PEP 8 style-checker (pep8.py) prefers B and warns if you use A; however, both variations are legal. My own view is that since Python will successfully interpret the code in either case that this doesn't really matter, and trying to enforce it would be a lot of work for very little gain. I suppose if you are very adamantly in favor of one or the other you could automatically convert the one to the other. Trying to fix all such lines manually, though, would be a huge undertaking and really not worth the effort, IMHO.

这篇关于“空行”中的Python缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆