代码这样写不止于优雅(Python 版)

开发
一份优雅、干净、整洁的代码通常自带文档和注释属性,读代码即是读作者的思路。Python 开发中很少要像 Java 一样把遵循某种设计模式作为开发原则来应用到系统中,毕竟设计模式只是一种实现手段而已,代码清晰才是最终目的,而 Python 灵活而不失优雅,这也是为什么 Python 能够深受 geek 喜爱的原因之一。

Martin(Bob大叔)曾在《代码整洁之道》一书打趣地说:当你的代码在做 Code Review 时,审查者要是愤怒地吼道:

“What the fuck is this shit?”

“Dude, What the fuck!”

等言辞激烈的词语时,那说明你写的代码是 Bad Code,如果审查者只是漫不经心的吐出几个

“What the fuck?”,

那说明你写的是 Good Code。衡量代码质量的唯一标准就是每分钟骂出“WTF” 的频率。 

 

 

 

一份优雅、干净、整洁的代码通常自带文档和注释属性,读代码即是读作者的思路。Python 开发中很少要像 Java 一样把遵循某种设计模式作为开发原则来应用到系统中,毕竟设计模式只是一种实现手段而已,代码清晰才是最终目的,而 Python 灵活而不失优雅,这也是为什么 Python 能够深受 geek 喜爱的原因之一。

上周写了一篇:《代码这样写更优雅》,朋友们纷纷表示希望再写点儿,今天就接着这个话题写点 Python 中那些 Pythonic 的写法,希望可以抛砖引玉。

1、链式比较操作

  1. age = 18 
  2.  
  3. if age > 18 and age < 60: 
  4.  
  5.     print("young man" 

pythonic

  1. if 18 < age < 60: 
  2.  
  3. print("young man" 

理解了链式比较操作,那么你应该知道为什么下面这行代码输出的结果是 False。

  1. >>> False == False == True 
  2.  
  3. False  

2、if/else 三目运算

  1. if gender == 'male'
  2.  
  3.     text = '男' 
  4.  
  5. else
  6.  
  7.     text = '女'  

pythonic

  1. text = '男' if gender == 'male' else '女' 

在类C的语言中都支持三目运算 b?x:y,Python之禅有这样一句话:

“There should be one– and preferably only one –obvious way to do it. ”。

能够用 if/else 清晰表达逻辑时,就没必要再额外新增一种方式来实现。

3、真值判断

检查某个对象是否为真值时,还显示地与 True 和 False 做比较就显得多此一举,不专业

  1. if attr == True
  2.  
  3.     do_something() 
  4.  
  5.   
  6.  
  7. if len(values) != 0: # 判断列表是否为空 
  8.  
  9.     do_something()  

pythonic

  1. if attr: 
  2.  
  3.     do_something() 
  4.  
  5.   
  6.  
  7. if values
  8.  
  9.     do_something()  

真假值对照表: 

 

 

 

4、for/else语句

for else 是 Python 中特有的语法格式,else 中的代码在 for 循环遍历完所有元素之后执行。

  1. flagfound = False 
  2.  
  3. for i in mylist: 
  4.  
  5.     if i == theflag: 
  6.  
  7.         flagfound = True 
  8.  
  9.         break 
  10.  
  11.     process(i) 
  12.  
  13.   
  14.  
  15. if not flagfound: 
  16.  
  17.     raise ValueError("List argument missing terminal flag." 

pythonic

  1. for i in mylist: 
  2.  
  3.     if i == theflag: 
  4.  
  5.         break 
  6.  
  7.     process(i) 
  8.  
  9. else
  10.  
  11.     raise ValueError("List argument missing terminal flag." 

5、字符串格式化

  1. s1 = "foofish.net" 
  2.  
  3. s2 = "vttalk" 
  4.  
  5. s3 = "welcome to %s and following %s" % (s1, s2)  

pythonic

  1. s3 = "welcome to {blog} and following {wechat}".format(blog="foofish.net", wechat="vttalk"

很难说用 format 比用 %s 的代码量少,但是 format 更易于理解。

“Explicit is better than implicit — Zen of Python”

6、列表切片

获取列表中的部分元素***想到的就是用 for 循环根据条件提取元素,这也是其它语言中惯用的手段,而在 Python 中还有强大的切片功能。

  1. items = range(10) 
  2.  
  3.   
  4.  
  5. # 奇数 
  6.  
  7. odd_items = [] 
  8.  
  9. for i in items: 
  10.  
  11.     if i % 2 != 0: 
  12.  
  13.         odd_items.append(i) 
  14.  
  15.   
  16.  
  17. # 拷贝 
  18.  
  19. copy_items = [] 
  20.  
  21. for i in items: 
  22.  
  23.     copy_items.append(i)  

pythonic

  1. # 第1到第4个元素的范围区间 
  2.  
  3. sub_items = items[1:4] 
  4.  
  5. # 奇数 
  6.  
  7. odd_items = items[1::2] 
  8.  
  9. #拷贝 
  10.  
  11. copy_items = items[::] 或者 items[:]  

列表元素的下标不仅可以用正数表示,还是用负数表示,***一个元素的位置是 -1,从右往左,依次递减。

  1. -------------------------- 
  2.  
  3. | P | y | t | h | o | n | 
  4.  
  5. -------------------------- 
  6.  
  7.    0   1   2   3   4   5 
  8.  
  9.   -6  -5  -4  -3  -2  -1 
  10.  
  11. --------------------------  

7、善用生成器

  1. def fib(n): 
  2.  
  3.     a, b = 0, 1 
  4.  
  5.     result = [] 
  6.  
  7.      while b < n: 
  8.  
  9.         result.append(b) 
  10.  
  11.         a, b = b, a+b 
  12.  
  13.     return result  

pythonic

  1. def fib(n): 
  2.  
  3.     a, b = 0, 1 
  4.  
  5.     while a < n: 
  6.  
  7.         yield a 
  8.  
  9.         a, b = b, a + b  

上面是用生成器生成费波那契数列。生成器的好处就是无需一次性把所有元素加载到内存,只有迭代获取元素时才返回该元素,而列表是预先一次性把全部元素加载到了内存。此外用 yield 代码看起来更清晰。

8、获取字典元素

  1. d = {'name''foo'
  2.  
  3. if d.has_key('name'): 
  4.  
  5.     print(d['name']) 
  6.  
  7. else
  8.  
  9.     print('unknown' 

pythonic

  1. d.get("name""unknown"

9、预设字典默认值

通过 key 分组的时候,不得不每次检查 key 是否已经存在于字典中。

  1. data = [('foo', 10), ('bar', 20), ('foo', 39), ('bar', 49)] 
  2.  
  3. groups = {} 
  4.  
  5. for (key, value) in data: 
  6.  
  7.     if key in groups: 
  8.  
  9.         groups[key].append(value) 
  10.  
  11.     else
  12.  
  13.         groups[key] = [value] 

pythonic

  1. # ***种方式 
  2.  
  3. groups = {} 
  4.  
  5. for (key, value) in data: 
  6.  
  7.     groups.setdefault(key, []).append(value) 
  8.  
  9.   
  10.  
  11. # 第二种方式 
  12.  
  13. from collections import defaultdict 
  14.  
  15. groups = defaultdict(list) 
  16.  
  17. for (key, value) in data: 
  18.  
  19.     groups[key].append(value)  

10、字典推导式

在python2.7之前,构建字典对象一般使用下面这种方式,可读性非常差

  1. numbers = [1,2,3] 
  2.  
  3. my_dict = dict([(number,number*2) for number in numbers]) 
  4.  
  5. print(my_dict)  # {1: 2, 2: 4, 3: 6}  

pythonic

  1. numbers = [1, 2, 3] 
  2.  
  3. my_dict = {number: number * 2 for number in numbers} 
  4.  
  5. print(my_dict) # {1: 2, 2: 4, 3: 6} 
  6.  
  7. # 还可以指定过滤条件 
  8.  
  9. my_dict = {number: number * 2 for number in numbers if number > 1} 
  10.  
  11. print(my_dict) # {2: 4, 3: 6}  

字典推导式是python2.7新增的特性,可读性增强了很多,类似的还是列表推导式和集合推导式。 

责任编辑:庞桂玉 来源: Python开发者
相关推荐

2017-07-07 16:57:35

代码Python

2019-11-25 14:06:44

AI无人驾驶自动驾驶

2021-04-20 10:50:38

Spring Boot代码Java

2020-07-07 15:50:17

区块链互联网人工智能

2017-05-03 09:49:14

OpenStack私有云搭建

2020-04-03 14:55:39

Python 代码编程

2014-02-28 13:46:35

Angular代码

2022-06-22 16:31:26

阿里云数字化转型云原生

2016-02-23 17:50:38

认知计算IBM

2010-02-24 09:53:07

Zaurus Ubun

2018-03-07 15:27:57

三星笔记本

2020-09-27 10:55:10

代码Java字符串

2022-05-24 15:34:35

Commvault

2020-04-25 14:06:04

BGP网络攻击泄露

2022-04-18 09:31:21

数据库查询MySQL

2022-12-22 10:37:53

数字化自动化UiPath

2018-05-06 23:04:12

Android Chrome OS操作系统

2023-05-08 09:31:17

2017-01-06 13:45:45

智能 运动
点赞
收藏

51CTO技术栈公众号