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?
PEP8 says you have to surround top level functions with 2 lines, however if you were to have a constant/global variable there, instead of those functions it could have easily been 1 line.
Two blank lines should be both before and after class definitions. One blank line should be both before and after method definitions. You should use blank lines conservatively within your code to separate groups of functions.
Blank line is definitely allowed in python functions. All of yours examples (A,B,C,D) should work and the problem probably (sure) is related to "Eclipse and the PyDev plug-in".
The double-empty-line rule only applies to function and class definitions at the top level. Separate top-level function and class definitions with two blank lines. Use blank lines in functions, sparingly, to indicate logical sections.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With