本文介绍了交互式提示下的空行规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么在交互式提示和程序从外壳程序作为可执行文件运行时,Python中的空行会有不同的规则。

I was wondering why is there a different rule for blank lines in Python between interactive prompt and when the program is run from shell as an executable.

因为空行被忽略,我喜欢大量使用它们。但是,在交互式提示中,空白行用于终止循环。因此,当我将大量代码粘贴到交互式提示中时,由于在整个循环中都将出现空白行,所以我一直遇到缩进错误。因此,这使得交互式调试/开发过程有些繁琐。用#代替空白行会有所帮助,但我喜欢我的空白行。

Since blank lines are ignored, I enjoy using them abundantly. However, in the interactive prompt, a blank line is used to terminate a loop. Thus, I keep running into indentation errors when I paste a chunk of code into the interactive prompt as I would have blank lines throughout my loops. Consequently, this makes the interactive debugging/development process somewhat tedious. Putting a # instead of blank line helps, but I like my blank lines.

更令人讨厌的是提示之间的不同行为(例如python和ipython)。 python交互式提示会在我期望的地方给我一个错误,而ipython会继续执行缩进的代码,就好像它不是循环的一部分而不会抱怨。

Even more annoying is different behavior between prompts (e.g. python and ipython). Where python interactive prompt would give me an error where I expect it, ipython would keep executing the indented code as if it was not part of loop without complaining.

是一个简单的解决方案,但我不知道。我正在使用vi进行编辑和python / ipython提示。谢谢。

I feel there is an easy solution out there, but I'm not aware of it. I am using vi for editting and python/ipython prompts. Thanks.

推荐答案

定义应如何使用空白行:

PEP8 defines how blank lines should be used:

用两个空行分隔顶级函数和类定义。
类中的方法定义用单个空白行分隔。

Separate top-level function and class definitions with two blank lines. Method definitions inside a class are separated by a single blank line.

可以(分别)使用多余的空白行来分隔相关的
组功能。一堆相关的
单行代码(例如,一组虚拟实现)之间可以省略空白行。

Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).

在函数中使用空白行时要谨慎

Use blank lines in functions, sparingly, to indicate logical sections.

Python接受control-L(即^ L)换页字符作为空格;
许多工具将这些字符视为页面分隔符,因此您可以使用
来分隔文件相关部分的页面。请注意,某些编辑器
和基于Web的代码查看器可能不会将control-L视为换页
,并会在其位置显示另一个标志符号。

Python accepts the control-L (i.e. ^L) form feed character as whitespace; Many tools treat these characters as page separators, so you may use them to separate pages of related sections of your file. Note, some editors and web-based code viewers may not recognize control-L as a form feed and will show another glyph in its place.

如果您坚持使用PEP,那么在交互式控制台中唯一可能出现空白行的问题就是函数中的空白行指示逻辑部分。

If you stick to the PEP, the only instance in which blank lines might generate problems in the interactive console are "blank lines in functions to indicate logical sections", I believe.

您可以通过以下方式解决此问题:

You can circumvent the problem like this though:

>>> def a():
...     print 'foo'\
...
...     print 'bar'
...
>>> a()
foo
bar

编辑:请注意,按照您在问题中的建议,使用 of 代替留空行...(当 \ 进入上一个行)。

Note that using \ instead of # as you suggest in your question leaves the blank line... blank (as the \ goes on the previous line).

HTH!

这篇关于交互式提示下的空行规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:09