|
55 | 55 | 一个缩进级别四个空格。
|
56 | 56 | * 连续行使用两种方式使封装元素成为一行:括号内垂直隐式连接和悬挂式缩进,使用悬挂式缩进应该注意第一行不应该有参数,连续行要使用进一步的缩进来区分。
|
57 | 57 | ```Python
|
58 | | -# 垂直隐式连接对齐,用开放定界符使成一行 |
59 | | -foo = func_name(var_one, var_two, |
60 | | - var_three, var_four) |
61 | | - |
62 | | -# 悬挂式缩进,函数定义时需要再加一级缩进区分非参数代码行 |
| 58 | +# Aligned with opening delimiter. |
| 59 | +foo = long_function_name(var_one, var_two, |
| 60 | + var_three, var_four) |
| 61 | + |
| 62 | +# More indentation included to distinguish this from the rest. |
63 | 63 | def long_function_name(
|
64 | 64 | var_one, var_two, var_three,
|
65 | 65 | var_four):
|
66 | 66 | print(var_one)
|
67 | | - |
68 | | -# 函数调用时悬挂式一级缩进 |
| 67 | + |
| 68 | +# Hanging indents should add a level. |
69 | 69 | foo = long_function_name(
|
70 | 70 | var_one, var_two,
|
71 | 71 | var_three, var_four)
|
72 | 72 | ```
|
73 | 73 | * 当 if 语句过长需要换行时,以下处理方法可以采用。
|
74 | 74 | ```Python
|
75 | | -# 没有额外的缩进 |
| 75 | +# No extra indentation. |
76 | 76 | if (this_is_one_thing and
|
77 | 77 | that_is_another_thing):
|
78 | 78 | do_something()
|
79 | 79 |
|
80 | | -# 增加注释,用以区分函数体部分 |
81 | | -# 支持语法高亮 |
| 80 | +# Add a comment, which will provide some distinction in editors |
| 81 | +# supporting syntax highlighting. |
82 | 82 | if (this_is_one_thing and
|
83 | 83 | that_is_another_thing):
|
84 | 84 | # Since both conditions are true, we can frobnicate.
|
85 | 85 | do_something()
|
86 | 86 |
|
87 | | -# 连续行使用进一步的缩进和其他函数体区分 |
| 87 | +# Add some extra indentation on the conditional continuation line. |
88 | 88 | if (this_is_one_thing
|
89 | 89 | and that_is_another_thing):
|
90 | 90 | do_something()
|
@@ -134,4 +134,65 @@ income = (gross_wages
|
134 | 134 | Python 2 默认ASCII,Python 3 默认UTF-8。
|
135 | 135 | 使用 ASCII 的 Python 2 源文件或使用 UTF-8 的 Python 3 源文件不应该有编码声明。
|
136 | 136 | 源文件最好只使用 ASCII 字符,即使是蹩脚的 Chinglish 亦可,家和万事兴。
|
137 | | - |
| 137 | + |
| 138 | +<h5 id="3.7">模块导入</h5> |
| 139 | +```Python |
| 140 | +YES: from subprocess import Popen, PIPE |
| 141 | + import os |
| 142 | + import sys |
| 143 | + |
| 144 | +NO: import sys, os |
| 145 | +``` |
| 146 | +模块导入总是位于文件顶部,在模块注释和文档字符串之后,模块全局变量和常量之前 |
| 147 | +导入应该按照以下顺序分组,不同组间用空行隔离 |
| 148 | +* 标准库 imports |
| 149 | +* 相关第三方 imports |
| 150 | +* 本地特定应用/库 imports |
| 151 | +推荐使用绝对导入,标准库代码应总是使用绝对导入 |
| 152 | +```Python |
| 153 | +import mypkg.sibling |
| 154 | +from mypkg import sibling |
| 155 | +from mypkg.sibling import example |
| 156 | +``` |
| 157 | +在包结构比较复杂时,可以使用相对导入 |
| 158 | +```Python |
| 159 | +from . import sibling |
| 160 | +from .sibling import example |
| 161 | +``` |
| 162 | +在 Python 3 中,相对导入已经被删除,禁止使用 |
| 163 | +类导入 |
| 164 | +```Python |
| 165 | +from myclass import MyClass |
| 166 | +from foo.bar.yourclass import YourClass |
| 167 | +``` |
| 168 | +如果这种方式导致了本地命名冲突,可以使用以下方式 |
| 169 | +```Python |
| 170 | +import myclass |
| 171 | +import foo.bar.yourclass |
| 172 | +``` |
| 173 | +然后使用 myclass.MyClass 和 foo.bar.yourclass.YourClass |
| 174 | +请不要使用以下方式 |
| 175 | +```Python |
| 176 | +from <module> import * |
| 177 | +``` |
| 178 | +<h5 id="3.8">模块级别 dunder 名称</h5> |
| 179 | +模块级别 "dunders"(即具有两个前导和两个后缀下划线的名称),例如 \_\_all\_\_,\_\_author\_\_,\_\_version\_\_ 等应放在模块 docstring 之后,但在任何 import 语句之前,但是除了 \_\_future\_\_ 导入。 Python 强制 future-imports 必须在除了 docstrings 之外的任何其他代码之前出现在模块中。 |
| 180 | +例如: |
| 181 | +```Python |
| 182 | +"""This is the example module. |
| 183 | + |
| 184 | +This module does stuff. |
| 185 | +""" |
| 186 | + |
| 187 | +from __future__ import barry_as_FLUFL |
| 188 | + |
| 189 | +__all__ = ['a', 'b', 'c'] |
| 190 | +__version__ = '0.1' |
| 191 | +__author__ = 'Cardinal Biggles' |
| 192 | + |
| 193 | +import os |
| 194 | +import sys |
| 195 | +``` |
| 196 | +<h5 id="4">字符串引号</h5> |
| 197 | +在 Python 中,单引号和双引号是等价的,只需要坚持使用一种并保持一致即可。 |
| 198 | +在双引号中使用单引号,单引号中使用双引号。三引号中使用双引号。 |
0 commit comments