也许,时间见过我们
posts - 97,comments - 14,views - 24万

让代码更Pythonic

 

当然不要过分追求简洁,不然阅读的人就遭殃了,

部分逻辑复杂的情况还应按照清晰的逻辑脉络去写方便阅读,

毕竟我们是用代码实现功能然后维护,而不是单单的炫技。

 

##############

这里博主收集了一些比较常见的 Pythonic 写法,帮助你养成写优秀代码的习惯

1. 交换两个变量的值,正常都会想利用一个中间临时变量来过渡。

能用一行代码解决的(并且不影响可读性的),决不用三行代码

1 tmp = a
2 a = b
3 b = tmp
4 
5 ##############
6 a,b = b,a

 

2.在一个 for 循环中,如果逻辑比较简单,不如试用一下列表的列表推导式,虽然只有一行代码,但也逻辑清晰

1 my_list = []
2 for i in range(10):
3     my_list.append(i*2)
4 
5 ##################
6 my_list = [i*2 for i in range(10)]

 

3.带索引遍历,更好的做法是利用 enumerate 这个内置函数

1 for i in range(len(my_list)):
2     print(i, "-->", my_list[i])
3 
4 ###############
5 for i,item in enumerate(my_list):
6     print(i, "-->",item)

 

4.序列解包,使用 * 可以对一个列表解包

1 a, *rest = [1, 2, 3]
2 # a = 1, rest = [2, 3]
3 
4 #################
5 a, *middle, c = [1, 2, 3, 4]
6 # a = 1, middle = [2, 3], c = 4

 

5.字符串拼接,一个列表(或者可迭代对象)中的所有元素都是字符串对象,想要将他们连接起来,更推荐的做法是使用 join 函数

1 letters = ['s', 'p', 'a', 'm']
2 s=""
3 for let in letters:
4     s += let
5 
6 ###############
7 letters = ['s', 'p', 'a', 'm']
8 word = ''.join(letters)

 

6.判断一个变量是否为真(假),新手习惯直接使用 == 与 True、False、None 进行对比,实际上,""、[]、{} 这些没有任何元素的容器都是假值,可直接使用 if not xx 来判断。

复制代码
 1 if attr == True:
 2     print('True!')
 3 
 4 if attr == None:
 5     print('attr is None!')
 6 
 7 
 8 #################
 9 if attr:
10     print('attr is truthy!')
11 
12 if not attr:
13     print('attr is falsey!')
复制代码

 

7.当直接使用 [] 来访问字典里的元素时,若key不存在,是会抛异常的,所以新会可能会先判断一下是否有这个 key,有再取,

更推荐的做法是使用 get 来取,如果没有该 key 会默认返回 None(当然你也可以设置默认返回值)

复制代码
 1 d = {'hello': 'world'}
 2 if d.has_key('hello'):
 3     print(d['hello'])    # prints 'world'
 4 else:
 5     print('default_value')
 6 
 7 ######################
 8 d = {'hello': 'world'}
 9 
10 print(d.get('hello', 'default_value')) # prints 'world'
11 print(d.get('thingy', 'default_value')) # prints 'default_value'
复制代码

 

8.可以使用列表推导或者高阶函数 filter 来实现过滤列表元素,除了 filter 之外,还有 map、reduce 这两个函数也很好用

复制代码
 1 a = [3, 4, 5]
 2 b = []
 3 for i in a:
 4     if i > 4:
 5         b.append(i)
 6 ##############
 7 a = [3, 4, 5]
 8 b = [i for i in a if i > 4]
 9 # Or:
10 b = filter(lambda x: x > 4, a)
11 
12 a = [3, 4, 5]
13 b = map(lambda i: i + 3, a)
14 # b: [6,7,8]
复制代码

 

9.文件读取是非常常用的操作,在使用完句柄后,是需要手动调用 close 函数来关闭句柄的,

如果代码写得太长,即使你知道需要手动关闭句柄,却也会经常会漏掉。因此推荐养成习惯使用 with open 来读写文件,上下文管理器会自动执行关闭句柄的操作

1 fp = open('file.txt')
2 print(fp.read())
3 fp.close()
4 
5 #############
6 with open('file.txt') as fp:
7     for line in fp.readlines():
8         print(line)

 

10.代码续航,将一个长度较长的字符串放在一行中,是很影响代码可读性的(下面代码可向左滑动),

稍等注重代码可读性的人,会使用三个引号 \来续写,也可以使用括号,导包的时候亦是如此

复制代码
 1 long_string = 'For a long time I used to go to bed early. Sometimes, when I had put out my candle, my eyes would close so quickly that I had not even time to say “I’m going to sleep.”'
 2 
 3 ####################################
 4 long_string = 'For a long time I used to go to bed early. ' \
 5               'Sometimes, when I had put out my candle, ' \
 6               'my eyes would close so quickly that I had not even time to say “I’m going to sleep.”'
 7 
 8 #or
 9 long_string = (
10     "For a long time I used to go to bed early. Sometimes, "
11     "when I had put out my candle, my eyes would close so quickly "
12     "that I had not even time to say “I’m going to sleep.”"
13 )
14 ###############################
15 from some.deep.module.inside.a.module import (
16     a_nice_function, another_nice_function, yet_another_nice_function)
复制代码

 

11.有时候出于需要,我们会使用一些特殊的魔法来使代码适应更多的场景不确定性,

但若非必要,请不要那么做。无端增加代码的不确定性,会让原先本就动态的语言写出更加动态的代码

1 def make_complex(*args):
2     x, y = args
3     return dict(**locals())
4 
5 ################
6 def make_complex(x, y):
7     return {'x': x, 'y': y}

 

12.对于暂不需要,却又不得不接收的的变量,请使用占位符

 1 filename = 'foobar.txt' 2 basename, _, ext = filename.rpartition('.') 

 

13.对于简单的判断并赋值,其实是可以使用三目运算,一行搞定

1 age = 20
2 if age > 18:
3     type = "adult"
4 else:
5     type = "teenager"
6 
7 ###############
8 age = 20  b = "adult" if age > 18 else "teenager"

 

posted on   画入新雪  阅读(565)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Copyright ©2017 画入新雪
点击右上角即可分享
微信分享提示