加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

[转]如何像Python高手(Pythonista)一样编程

发布时间:2020-05-27 12:40:31 所属栏目:Python 来源:互联网
导读:本文转自:http://xianglong.me/article/how-to-code-like-a-pythonista-idiomatic-python最近在网上看到一篇介绍Pythonic编程的文章:Code Like a Pythonista: Idiomatic Python,其实作者在2006的PyCon会议后就写了这篇文章,写这篇文章的主要原因是作者发

本文转自:

最近在网上看到一篇介绍Pythonic编程的文章:,其实作者在2006的PyCon会议后就写了这篇文章,写这篇文章的主要原因是作者发现很多有经验的Pythoner写出的代码不够Pythonic。我觉得这篇文章很不错,所以将它用中文写了下来(不是逐字的翻译,中间加了一些自己的理解),分享给大家。另:由于本人平时时间有限,这篇文章翻译了比较长的时间,如果你发现了什么不对的地方,欢迎指出。。

一、Python之禅(The Zen of Python)

The Zen of Python是Python语言的指导原则,遵循这些基本原则,你就可以像个Pythonista一样编程。具体内容你可以在Python命令行输入import this看到:

Beautiful <span style="color: #0000ff;">is<span style="color: #000000;"> better than ugly.
<span style="color: #008000;">#<span style="color: #008000;"> 优美胜于丑陋(Python以编写优美的代码为目标)
<span style="color: #000000;">
Explicit <span style="color: #0000ff;">is<span style="color: #000000;"> better than implicit.
<span style="color: #008000;">#<span style="color: #008000;"> 明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
<span style="color: #000000;">
Simple <span style="color: #0000ff;">is<span style="color: #000000;"> better than complex.
<span style="color: #008000;">#<span style="color: #008000;"> 简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
<span style="color: #000000;">
Complex <span style="color: #0000ff;">is<span style="color: #000000;"> better than complicated.
<span style="color: #008000;">#<span style="color: #008000;"> 复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
<span style="color: #000000;">
Flat <span style="color: #0000ff;">is<span style="color: #000000;"> better than nested.
<span style="color: #008000;">#<span style="color: #008000;"> 扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
<span style="color: #000000;">
Sparse <span style="color: #0000ff;">is<span style="color: #000000;"> better than dense.
<span style="color: #008000;">#<span style="color: #008000;"> 间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
<span style="color: #000000;">
Readability counts.
<span style="color: #008000;">#<span style="color: #008000;"> 可读性很重要(优美的代码是可读的)
<span style="color: #000000;">
Special cases aren<span style="color: #800000;">'<span style="color: #800000;">t special enough to break the rules.
<span style="color: #000000;">Although practicality beats purity.
<span style="color: #008000;">#<span style="color: #008000;"> 即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
<span style="color: #000000;">
Errors should never <span style="color: #0000ff;">pass<span style="color: #000000;"> silently.
Unless explicitly silenced.
<span style="color: #008000;">#<span style="color: #008000;"> 不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写except:pass风格的代码)
<span style="color: #000000;">
In the face of ambiguity,refuse the temptation to guess.
<span style="color: #008000;">#<span style="color: #008000;"> 当存在多种可能,不要尝试去猜测
<span style="color: #000000;">
There should be one-- <span style="color: #0000ff;">and preferably only one --<span style="color: #000000;">obvious way to do it.
<span style="color: #008000;">#<span style="color: #008000;"> 而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)
<span style="color: #000000;">
Although that way may <span style="color: #0000ff;">not be obvious at first unless you<span style="color: #800000;">'<span style="color: #800000;">re Dutch.
<span style="color: #008000;">#<span style="color: #008000;"> 虽然这并不容易,因为你不是 Python 之父(这里的Dutch是指Guido)
Now <span style="color: #0000ff;">is<span style="color: #000000;"> better than never.
Although never <span style="color: #0000ff;">is often better than right<span style="color: #000000;"> now.
<span style="color: #008000;">#<span style="color: #008000;"> 做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)
If the implementation <span style="color: #0000ff;">is hard to explain,it<span style="color: #800000;">'<span style="color: #800000;">s a bad idea.
If the implementation <span style="color: #0000ff;">is<span style="color: #000000;"> easy to explain,it may be a good idea.
<span style="color: #008000;">#<span style="color: #008000;"> 如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)
Namespaces are one honking great idea -- let<span style="color: #800000;">'<span style="color: #800000;">s do more of those!
<span style="color: #008000;">#<span style="color: #008000;"> 命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)

二、PEP8: Python编码规范(PEP8: Style Guide for Python Code)

空格和缩进(WhiteSpace and Indentation)

空格和缩进在Python语言中非常重要,它替代了其他语言中{}的作用,用来区分代码块和作用域。在这方面PEP8有以下的建议:

123456、紧随括号后面或者参数列表前一个字符不要存在空格

Python命名

1、方法 &2、常量:joined_lower 34、类属性:interface,_internal, 5、camelCase only to conform to pre-existing conventions

三、交换变量值(Swap Values)

在其他语言中,交换两个变量值的时候,可以这样写:

temp === temp

b,a = a,b

1、Python会先将右边的a,b生成一个tuple(元组),存放在内存中;

2、之后会执行赋值操作,这时候会将tuple拆开;

3、然后将tuple的第一个元素赋值给左边的第一个变量,第二个元素赋值给左边第二个变量。

再举个tuple拆分的例子:

In [1]: people = [,,In [2]: name,title,phone =<span style="color: #000000;"> people

In [3<span style="color: #000000;">]: name
Out[3]: <span style="color: #800000;">'<span style="color: #800000;">David<span style="color: #800000;">'<span style="color: #000000;">

In [4<span style="color: #000000;">]: title
Out[4]: <span style="color: #800000;">'<span style="color: #800000;">Pythonista<span style="color: #800000;">'<span style="color: #000000;">

In [5<span style="color: #000000;">]: phone
Out[5]: <span style="color: #800000;">'<span style="color: #800000;">15145551234<span style="color: #800000;">'

In [6]: people = [[,],[,,In [7]: <span style="color: #0000ff;">for name,phone <span style="color: #0000ff;">in<span style="color: #000000;"> people:
...: <span style="color: #0000ff;">print<span style="color: #000000;"> name,phone
...:
David 15145551234<span style="color: #000000;">
Wu 15101365547

更多tuple的例子:

>>> 11>>> (11>>> (11 >>> value = 1>>>1,)

四、Python控制台的"_"(Interactive "_")

>>> >>> math.pi / 3 1.0471975511965976 >>> angle =>>>0.50000000000000011 >>>0.50000000000000011

五、合并字符串(Building Strings from Sub strings)

colors = [,,,result = <span style="color: #800000;">' '
<span style="color: #0000ff;">for s <span style="color: #0000ff;">in<span style="color: #000000;"> colors:
result += s

result = .join(colors)

六、使用关键字in(Use in where possible)

d = {: 1,: 2 d.has_key(<span style="color: #0000ff;">for key <span style="color: #0000ff;">in<span style="color: #000000;"> d:
<span style="color: #0000ff;">print<span style="color: #000000;"> key
<span style="color: #008000;">#<span style="color: #008000;"> DO NOT USE
<span style="color: #0000ff;">for key <span style="color: #0000ff;">in<span style="color: #000000;"> d.keys():
<span style="color: #0000ff;">print key

七、字典(Dictionary)

1、get

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读