Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 273
milancurcic Avatar asked Dec 05 '11 20:12

milancurcic


Video Answer


2 Answers

PEP8 defines how blank lines should be used:

Blank Lines

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 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.

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

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

HTH!

like image 39
mac Avatar answered Nov 08 '22 07:11

mac


...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.

like image 123
Wyrmwood Avatar answered Nov 08 '22 09:11

Wyrmwood