社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
所有的 Python 脚本文件都应在文件头标上如下标识或其兼容格式的标识:
# -*- coding:utf-8 -*-
设置编辑器,默认保存为 utf-8 格式。
##二、注释
2
个空格; #
号后面要空一格。##三、格式
###1. 缩进
4 个空格
进行缩进。垂直对齐换行的元素
, 或者使用4空格的悬挂式缩进
**(这时第一行不应该有参数
):Yes: # Aligned with opening delimiter
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Aligned with opening delimiter in a dictionary
foo = {
long_dictionary_key: value1 +
value2,
...
}
# 4-space hanging indent; nothing on first line
foo = long_function_name(
var_one, var_two, var_three,
var_four)
# 4-space hanging indent in a dictionary
foo = {
long_dictionary_key:
long_dictionary_value,
...
}
No: # Stuff on first line forbidden
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 2-space hanging indent forbidden
foo = long_function_name(
var_one, var_two, var_three,
var_four)
# No hanging indent in a dictionary
foo = {
long_dictionary_key:
long_dictionary_value,
...
}
###2. 空格
a = b + c
d = {'key': 'value'}
magic(r=real, i=imag) if i & (i-1) == 0 # 括号内的减号,其两侧也不需要空格
###3. 空行:顶级定义之间空两行, 方法定义之间空一行
the class line and the first method
.within functions or methods
.###4. 断行
class Edit(Widget):
def __init__(self, parent, width,
font = FONT, color = BLACK,
pos = POS, style = 0):
pass
CapWords
CAPS_WITH_UNDER
lower_with_under
在最前面加一个下划线
,私有的实例属性一般在最前面加两个下划线
##五、语句
import 语句有以下几个原则需要遵守
标准库
,再 import 第三方库
,最后 import 自己开发的项目中的其它库文件或者模块
,这几种库中间用空行分隔开来。分枝和循环
if len(alist) != 0: do_something()
if alist != []: do_something()
if s != "": do_something()
if var != None: do_something()
if var != False: do_something()
# 注意,(seq, val)本身具有可判断性,所以,上面的语句应该写成:
if seq:
do_something()
if var:
do_something()
TODO注释应该在所有开头处包含”TODO”字符串, 紧跟着是用括号括起来的你的名字, email地址或其它标识符. 然后是一个可选的冒号. 接着必须有一行注释, 解释要做什么. 主要目的是为了有一个统一的TODO格式, 这样添加注释的人就可以搜索到(并可以按需提供更多细节).
# TODO(kl@gmail.com): Use a "*" here for string repetition.
# TODO(Zeke): Change this to use relations.
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!