1

I've been writing Python code for roughly 3-4 months now. I usually write in Atom but recently decided to try out PyCharm after seeing many good things about it. While writing some functions, I noticed I was getting errors and I didn't know why. According to PyCharm, functions should have two newlines after them?

I'd never seen this, or at least never noticed it before. I decided to Google some sample Python code and some pictures showed code with two newlines, but some only had one. Is there a generally accepted way of going about this or is it down to user preference?

  • You only *need* an extra newline if the function doesn't end with a `return`. I always use one anyway just to be consistent and to provide visual separation. Edit: and of course I missed the fact that you were asking about *two* blank lines. – Mark Ransom Dec 22 '16 at 19:24
  • It looks like it's not necessary, but recommended by PEP8: http://stackoverflow.com/questions/33466860/expected-two-blank-lines-pep8-warning-in-python – Will Dec 22 '16 at 19:24
  • Many of the "errors" that IDEs like PyCharm report are not really errors, but programming guidelines. – Barmar Dec 22 '16 at 19:40
  • Also, I won't be giving you error (marked as red) but a suggestion instead (marked as yellow) – Moinuddin Quadri Dec 22 '16 at 19:48

1 Answers1

4

It is considered as a good practice. As per PEP-0008 document:

Surround top-level function and class definitions with two blank lines.

Method definitions inside a class are surrounded 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.

PEP-8 is a Style Guide for Python Code

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126