官术网_书友最值得收藏!

Formatting recommendations

The code in the following screenshot demonstrates almost all the PEP 8 formatting recommendations:

I'll now walk us through the recommendations one by one:

  • PEP 8 recommends that a single line of code should not exceed a width of 79 characters
While this is consistent with displaying the code on a standard text mode interface, the primary reason for this rule in the modern world of widescreens and resizable windows is that it helps with reading. Even in contexts that have nothing to do with programming, layout designers prefer to limit line width.
  • Import statements should be placed at the top of the file, with standard library imports first, third-party imports next, and then imports from other modules within the same project
  • There should be a blank line between each group of imports
  • Classes and functions at the top level should have two blank lines separating them.
  • Methods within a class should have one blank line separating them
  • Within a function or method, blank lines should be used to indicate separation between conceptual groupings of code
  • Don't insert extra spaces before or after parentheses, brackets, or braces; and don't insert spaces before commas or colons
  • Always put a single space on either side of a binary operator, such as + or /
  • Don't put more than one statement on the same line, which is occasionally possible, but never a good idea
  • Comments should be written in human language, using that language's correct grammar
Preferably, that language should be English if you're going to allow your source code into the wild because this language is common to the majority of Python programmers.
  • Comments should also precede the section of code they described and be indented to the same level
  • Every public module, class, function, or method should have a properly formatted docstring
We'll look at what properly formatted means for docstrings in the Getting the most out of docstrings section of this chapter.

Let's move on to picking names for variables, functions, methods, classes, modules, packages, and so on.

主站蜘蛛池模板: 中方县| 辽阳县| 福海县| 西乌珠穆沁旗| 筠连县| 赞皇县| 隆林| 石景山区| 高碑店市| 濮阳市| 宜兰县| 班玛县| 宣武区| 水城县| 南雄市| 新昌县| 碌曲县| 永吉县| 台湾省| 特克斯县| 高淳县| 阳高县| 青田县| 拉萨市| 阳信县| 武定县| 景洪市| 美姑县| 通州市| 开阳县| 齐河县| 临泉县| 荣成市| 通城县| 铜鼓县| 繁昌县| 乌鲁木齐县| 云梦县| 陆丰市| 崇义县| 龙南县|