简体   繁体   中英

Pythonic way of writing multiple functions?

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?

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 (eg a set of dummy implementations).

Use blank lines in functions, sparingly, to indicate logical sections.

PEP-8 is a Style Guide for Python Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM