Pep 8 has the following rules
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.
However, you can’t have a completely blank line inside a class defintion
Example from my head:
class bunny:
def spam(self):
pass
def eggs(self):
pass
#a second example
class bunny2:
def __init__(self):
self._eggs = None
def eggs(self):
doc = "Spam and Eggs"
def fget(self, value):
return self._eggs
def fset(self, value):
self._eggs = value
def fdel(self):
del self._eggs
return locals()
eggs = property(**eggs())
The line between spam and eggs needs to be a blank line, however, that will result in a parse error of unexpected indentation. Is there another character that should go in that space? My assumption is just leave the spaces/tabs on the “blank” line because it is more readable.
In the second example nested defs need to have their previous lines indention maintained for the parse to work correctly.
What is the correct PEP 8 way to handle this? Blank line, blank line with white space, no line?
If you’re working in the REPL, you can’t have entirely blank lines. But there is no reason for code entered in the REPL to strictly adhere to PEP 8, anyway. But inside a file, it is a good idea to follow PEP 8.