Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

python - Blank line rule at interactive prompt

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.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

...why is there a different rule for blank lines in Python between interactive prompt and when the program is run from shell

Because, the interpreter tries to execute as soon as you hit return, but it needs the blank line to know your function, loop, if statement or other indented block is finished. If writing a function, it doesn't need (in fact won't work) if you add blank lines before the last line of the function. In this case, the blank line is needed to signal the end of the function.

You can execute a script with no blank lines from the shell, for example

for i in range(3):
    print i
a = 1000
print a

Would produce

$ python test.py
0
1
2
1000

But if you paste this in the interpreter, you'll get

>>> for i in range(3):
...     print i
... a = 1000
  File "<stdin>", line 3
a = 1000
^
SyntaxError: invalid syntax
>>> print a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

If you add a blank line to signify the end of the loop

for i in range(3):
    print i

a = 1000
print a

and paste it in the interpreter,

>>> for i in range(3):
...     print i
...
0
1
2
>>> a = 1000
>>> print a
1000

And that blank line must be blank, even spaces (perhaps auto added by your editor) will cause the interpreter to fail.

If you want to paste a class into the interpreter, then you don't want any spaces between any lines, including between methods. This raises a conflict with PEP8, however, meaning, you can either comply with PEP8, or be interpreter compatible, but not both.

Thus, if you want to be able to copy and paste your code into the standard python interpreter, you'll need a slightly different set of rules.

  • Surround top-level function and class definitions with two blank lines.
  • A blank line is required for top level module code (outside a function or class) to end an indented block, such as a for loop, try/except or if statement.
  • Blank lines may not be used within a function, class or method (barring or #).

Stick to those and you'll retain the ability to paste into the interpreter. For a class, however, you won't be strictly PEP8 as a blank line is required before and after a method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...