Blank line Python PEP8 best practice in class definition
Solution 1
This is really a matter of taste. I personally include the blank line to be consisted with classes which have a docstring. Quoting PEP-0257:
Insert a blank line before and after all docstrings (one-line or multi-line) that document a class -- generally speaking, the class's methods are separated from each other by a single blank line, and the docstring needs to be offset from the first method by a blank line; for symmetry, put a blank line between the class header and the docstring.
To illustrate:
class WithoutDocString(object):
def __init__(self):
pass
class WithADocString(object):
"""Summary line.
Bla bla bla bla.
"""
def __init__(self):
pass
Solution 2
As I understand the blank line section of PEP-8, there is some liberty on this question. Blank lines may appear in some places (separating groups of related functions) and may be omitted in other places (to group a list of one-liners).
There is no liberty, however, about blank lines after definition headers. They should not appear, conforming to the PEP-8 rules.
Your PEP-8 compliance checker does not seem to check this, though.
Generally (not PEP-8 related), I have the feeling that blank lines, as many other formatting issues, is a matter of what you are used to. There are no scientific researches I know of that show which formatting works best on unbiased developers. And most of us are biased anyway, so even this probably would not mean very much.
When editing existing code, my main approach always is to stick to the existing formatting. But that's beside the point here ;-)
Related videos on Youtube
Caumons
Python & Django developer. Author of the book #ReiníciaTech.
Updated on July 12, 2022Comments
-
Caumons almost 2 years
I always leave a blank line after class definition and the code seems to be PEP8 compliant, as no warning is raised for this. I do this because I found it more readable than writing all together.
class Area(BaseModel): name = models.CharField(_("Name"), max_length=30) slug = models.SlugField(_("Slug"), max_length=30, unique=True) class Meta(BaseModel.Meta): verbose_name = _("Area") verbose_name_plural = _("Areas") ordering = [ "name", ]
However, when I read PEP8 code compliant. This extra space is never there and this code would look like this:
class Area(BaseModel): name = models.CharField(_("Name"), max_length=30) slug = models.SlugField(_("Slug"), max_length=30, unique=True) class Meta(BaseModel.Meta): verbose_name = _("Area") verbose_name_plural = _("Areas") ordering = [ "name", ]
My question is: Is that a "bad practice" what I'm doing. Should I avoid this extra blank lines in Python?
-
TyrantWave almost 11 yearsWell, for starters, you have no doctext. Way I do it, if my doctext is only 1 line, I don't include a blank line. If the doctext is multiline, then I add a blank line.
-
Caumons almost 11 years@TyrantWave, so if you haven't doctext or it only has one line then you don't leave a blank line. Else, you have a blank line. Is there any convention for this? I've already seen what you say.
-
-
Caumons almost 11 yearsIt's really curious that in the link you posted you can find your cuoted explanation, but in their own examples they don't follow it! If you realise, they're writing all together: method definition, docstring and code. So I think that this is really a matter of taste and it's still PEP8 compliant.
-
Caumons almost 11 yearsI'm using Pydev in Eclipse with the PEP8 validation enabled. I already read the blank line section and nothing is written about what I asked here. But it seems to be answered in @icecrime's answer.
-
icecrime almost 11 yearsThe PEP shows no example of a
class
definition, only functions :( -
Caumons almost 11 yearsWhen dealing with functions and methods, do you leave blank lines when having docstrings? As it says, in
all docstrings
... -
ARF over 9 yearsNote that PEP-0257 seems to have changed. The section "; for symmetry, put a blank line between the class header and the docstring." has been deleted.
-
Jmills almost 9 years