Python由于语言的简洁性,让我们以人类思考的方式来写代码,新手更容易上手,老鸟更爱不释手。
要写出 Pythonic(优雅的、地道的、整洁的)代码,还要平时多观察那些大牛代码,Github 上有很多非常优秀的源代码值得阅读,比如:requests、flask、tornado,这里小明收集了一些常见的 Pythonic 写法,帮助你养成写优秀代码的习惯。
01. 变量交换
Bad
<code class="language-python">tmp = a a = b b = tmp </code>
www#gaodaima.com来源gaodaima#com搞(代@码网搞代码
Pythonic
<code class="language-python">a,b = b,a </code>
02. 列表推导
Bad
<code class="language-python">my_list = [] for i in range(10): my_list.append(i*2) </code>
Pythonic
<code class="language-python">my_list = [i*2 for i in range(10)] </code>
03. 单行表达式
虽然列表推导式由于其简洁性及表达性,被广受推崇。
但是有许多可以写成单行的表达式,并不是好的做法。
Bad
<code class="language-python">print "one"; print "two" if x == 1: print "one" if <complex comparison> and <other complex comparison>: # do something </code>
Pythonic
<code class="language-python">print "one" print "two" if x == 1: print "one" cond1 = <complex comparison> cond2 = <other complex comparison> if cond1 and cond2: # do something </code>
04. 带索引遍历
Bad
<code class="language-python">for i in range(len(my_list)): print(i, "-->", my_list[i]) </code>
Pythonic
<code class="language-python">for i,item in enumerate(my_list): print(i, "-->",item) </code>
05. 序列解包
Pythonic
<code class="language-python">a, *rest = [1, 2, 3] # a = 1, rest = [2, 3] a, *middle, c = [1, 2, 3, 4] # a = 1, middle = [2, 3], c = 4 </code>
06. 字符串拼接
Bad
<code class="language-python">letters = ["s", "p", "a", "m"] s="" for let in letters: s += let </code>
Pythonic
<code class="language-python">letters = ["s", "p", "a", "m"] word = "".join(letters) </code>
07. 真假判断
Bad
<code class="language-python">if attr == True: print "True!" if attr == None: print "attr is None!" </code>
Pythonic
<code class="language-python">if attr: print "attr is truthy!" if not attr: print "attr is falsey!" if attr is None: print "attr is None!" </code>
08. 访问字典元素
Bad
<code class="language-python">d = {"hello": "world"} if d.has_key("hello"): print d["hello"] # prints "world" else: print "default_value" </code>
Pythonic
<code class="language-python">d = {"hello": "world"} print d.get("hello", "default_value") # prints "world" print d.get("thingy", "default_value") # prints "default_value" # Or: if "hello" in d: print d["hello"] </code>
09. 操作列表
Bad
<code class="language-python">a = [3, 4, 5] b = [] for i in a: if i > 4: b.append(i) </code>
Pythonic
<code class="language-python">a = [3, 4, 5] b = [i for i in a if i > 4] # Or: b = filter(lambda x: x > 4, a) </code>
Bad
<code class="language-python">a = [3, 4, 5] for i in range(len(a)): a[i] += 3 </code>
Pythonic
<code class="language-python">a = [3, 4, 5] a = [i + 3 for i in a] # Or: a = map(lambda i: i + 3, a) </code>
10. 文件读取
Bad
<code class="language-python">f = open("file.txt") a = f.read() print a f.close() </code>
Pythonic
<code class="language-python">with open("file.txt") as f: for line in f: print line </code>
11. 代码续行
Bad
<code class="language-python">my_very_big_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.”""" from some.deep.module.inside.a.module import a_nice_function, another_nice_function, yet_another_nice_function </code>
Pythonic
<code class="language-python">my_very_big_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.”" ) from some.deep.module.inside.a.module import ( a_nice_function, another_nice_function, yet_another_nice_function) </code>
12. 显式代码
Bad
<code class="language-python">def make_complex(*args): x, y = args return dict(**locals()) </code>
Pythonic
<code class="language-python">def make_complex(x, y): return {"x": x, "y": y} </code>
13. 使用占位符
Pythonic
<code class="language-python">filename = "foobar.txt" basename, _, ext = filename.rpartition(".") </code>
14. 链式比较
Bad
<code class="language-python">if age > 18 and age < 60: print("young man") </code>
Pythonic
<code class="language-python">if 18 < age < 60: print("young man") </code>
理解了链式比较操作,那么你应该知道为什么下面这行代码输出的结果是 False
<code>>>> False == False == True False </code>
15. 三目运算
这个保留意见。随使用习惯就好。
Bad
<code class="language-python">if a > 2: b = 2 else: b = 1 #b = 2 </code>
Pythonic
<code class="language-python">a = 3 b = 2 if a > 2 else 1 #b = 2 </code>
参考文档
文末福利
本人原创的 《PyCharm 中文指南》一书前段时间一经发布,就火爆了整个 Python 圈,发布仅一天的时间,下载量就突破了 1000 ,并且在当天就在 Github 上就收获了数百的 star,截至目前,下载量已经破万。
这本书一共将近 200 页,内含大量的图解,制作之精良,值得每个 Python 工程师 人手一份。
为方便你下载,我将这本书上传到 百度网盘上了,你可以自行获取。
链接:https://pan.baidu.com/s/1-NzATHFtaTV1MQzek70iUQ
密码:mft3