惯用语法
格式化
Python有一个语言定义的一组标准格式的规则,被称为PEP8。如果您正在浏览Python项目的提交信息,您可能会发现它们充满了PEP8清理信息。原因很简单:如果大家都同意一个共同的的命名和格式化公约,Python代码作为一个整体瞬间变得更容易被新手和经验丰富的开发人员接受。阅读PEP,在编辑器中安装一个PEP8的风格检查插件,并开始编写代码以其他的Python开发人员将共同遵循的方式。下面列出了几个例子。Identifier Type Format Example
类 骆驼拼写法 class StringManipulator:
变量 使用下划线连接词 words_joined_by_underscore = True
函数 使用下划线连接词 def are_words_joined_by_underscore(words):
私有类成员/函数 下划线作为前缀 def _update_statistics(self):
使用数据
交换两个变量时避免使用临时变量
在python中交换变量没有任何理由需要使用临时变量。我们可以使用元组,使我们的意图更加清晰。
不好的写法temp = foo
foo = bar
bar = temp
惯用的
(foo, bar) = (bar, foo)
使用元组来分配参数
在python中,能够实现多个元素同时赋值。那些熟悉LISP可能知道这是'desctructuring bind’。不好的写法
list_from_comma_separated_value_file = ['dog', 'Fido', 10]
animal = list_from_comma_separated_value_file[0]
name = list_from_comma_separated_value_file[1]
age = list_from_comma_separated_value_file[2]
惯用的
list_from_comma_separated_value_file = ['dog', 'Fido', 10]
(animal, name, age) = list_from_comma_separated_value_file
使用“.join”来连接一个单独的字符串列表中的元素
它的速度更快,使用更少的内存,你会看到它无处不反正。需要注意的是两个单引号表示我们创建的字符串列表元素之间的分隔符。不好的写法
result_list = ['True', 'False', 'File not found']
result_string = ''
for result in result_list:
result_string += result
惯用的
result_list = ['True', 'False', 'File not found']
result_string = ''.join(result_list)
在dict.get()中使用默认值
经常被忽视的get()的定义是默认的参数。如果不使用默认值(或collections.defaultdict类),你的代码将充斥着混乱。请记住,争取清晰。不好的写法
log_severity = None
if 'severity' in configuration:
log_severity = configuration['severity']
else:
log_severity = log.Info
惯用的
log_severity = configuration.get('severity', log.Info)
使用上下文管理,以确保资源得到妥善管理
类似的语言的RAII的原则,如C + +和D,上下文管理器(意味着要使用对象的语句),可以使资源管理更安全和更明确的。典型的例子是文件IO。不好的写法
file_handle = open(path_to_file, 'r')
for line in file_handle.readlines():
if some_function_that_throws_exceptions(line):
# do something
file_handle.close()
惯用的
with open(path_to_file, 'r') as file_handle:
for line in file_handle:
if some_function_that_throws_exceptions(line):
# do something
# No need to explicitly call 'close'. Handled by the File context manager
了解itertools模块的内容
如果你像StackOverflow频繁的网站,您可能会注意到,形式的问题的答案:“为什么Python不能有以下明显有用的库函数吗?” 几乎总是引用itertools模块。itertools提供的函数式编程的中坚分子,应被视为基本构建块。更重要的是,itertools的文档中有一个“食谱”一节,提供使用itertools模块地道的实现常见的功能性编程结构。出于某种原因,似乎知道的“食谱”一节的Python开发人员的数量微乎其微。事实上,使用惯用语法的编写代码的开发者必须要知道的一部分是itertools,尤其当你重新造轮子。控制结构
条件分支应避免被放置在同一行上
使用缩进来表示范围(如果在Python你已经这样做了),可以很容易地确定将被执行的条件语句的一部分。不好的写法
if name: print (name)
print address
惯用的
if name:
print (name)
print address
避免在一行中有多个语句
虽然语言的定义允许使用分号来让一行有多个语句,但是这样做没有道理的,因为代码更难阅读。通常情况下违背以前的规则。不好的写法
if this_is_bad_code: rewrite_code(); make_it_more_readable();
惯用的
if this_is_bad_code:
rewrite_code()
make_it_more_readable()
在复合的if语句中避免重复的变量名
当一个人想要检查的一些值,反复列出正在检查的变量,它的值是不必要的冗长。使用临时集合的意图明显。不好的写法
if name == 'Tom' or name == 'Dick' or name == 'Harry':
is_generic_name = True
惯用的
if name in ('Tom', 'Dick', 'Harry'):
is_generic_name = True
使用列表解析创建现有数据的子集列表
不好的写法some_other_list = range(1, 100)
my_weird_list_of_numbers = list()
for element in some_other_list:
if is_prime(element):
my_weird_list_of_numbers.append(element+5)
惯用的
some_other_list = range(1, 100)
my_weird_list_of_numbers = [element + 5 for element in some_other_list if is_prime(element)]
使用“in”关键字来遍历一个可迭代的对象
不好的写法my_list = ['Larry', 'Moe', 'Curly']
index = 0
while index < len(my_list):
print (my_list[index])
index+=1
惯用的
my_list = ['Larry', 'Moe', 'Curly']
for element in my_list:
print element
在循环中使用枚举函数,而不是创建一个“索引”变量
使用来自其他语言的程序员显式地声明一个变量来跟踪一个循环容器中的索引。例如,在C++for (int i=0; i < container.size(); ++i)
{
// Do stuff
}
在Python中,内建的函数 enumerate 能够实现
不好的写法
index = 0
for element in my_container:
print (index, element)
index+=1
惯用的
for index, element in enumerate(my_container):
print (index, element)