(点击上方公众号,可快速关注)来源:安生http://lovesoo.org/pythonic-python-programming.htmlPython最大的优点之一就是语法简洁,好的代码就像伪代码一样,干净、整洁、一目了然。要写出 Pythonic(优雅的、地道的、整洁的)代码,需要多看多学大牛们写的代码,github 上有很多非常优秀的源代码值得阅读,比如:requests、flask、tornado,下面列举一些常见的Pythonic写法。0. 程序必须先让人读懂,然后才能让计算机执行。“Programs must be written for people to read, and only incidentally for machines to execute.”1. 交换赋值##不推荐temp = aa = bb = a ##推荐a, b = b, a # 先生成一个元组(tuple)对象,然后unpack2. Unpacking##不推荐l = ['David', 'Pythonista', '+1-514-555-1234']first_name = l[0]last_name = l[1]phone_num
………………………………