Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank line Python PEP8 best practice in class definition [closed]

Tags:

python

pep8

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?

like image 827
Caumons Avatar asked Jun 25 '13 10:06

Caumons


People also ask

How many blank lines does the PEP8 Standard recommend to use between top level function definitions within a Python module?

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.

How many blank lines does the PEP8 Standard recommend to use between statements in a function definition in Python?

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.

Are Blank lines forbidden in Python?

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

How many blank lines should be before and after top level functions and class definitions?

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.


1 Answers

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
like image 170
icecrime Avatar answered Sep 22 '22 14:09

icecrime