Python 之禅与 Pythonic - PyTips 0x00

PyTips

项目地址:https://git.io/pytips

Python 之禅与 Pythonic

Python 之禅是 Python 语言的设计哲学与所倡导的编程理念,Pythonic 则是指基于 Python 理念编写更加符合 Python 语法习惯(idiomatic Python)的代码,这也是本项目所追求的目标,因此以本篇作为开头。

import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Python 之禅,by Tim Peters

优美胜于丑陋

明确胜于隐晦

简单胜于复杂

复杂胜于凌乱

扁平胜于嵌套

稀疏胜于紧凑

可读性至关重要

即便特例,也需服从以上规则

除非刻意追求,错误不应跳过

面对歧义条件,拒绝尝试猜测

解决问题的最优方法应该有且只有一个

尽管这一方法并非显而易见(除非你是Python之父)

动手胜于空想

空想胜于不想

难以解释的实现方案,不是好方案

易于解释的实现方案,才是好方案

命名空间是个绝妙的理念,多多益善!

参考

  1. 《Python之禅》的翻译和解释
  2. What is Pythonic?
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在vim的命令模式下输入: :read !echo import this | python 会得到 The Ze...
    hjqjk阅读 4,351评论 0 1
  • 我正在编辑一本互联网技术的书稿,《Python编程基础与HTTP接口测试》。这本书不厚,因为作者说,我想教给大家的...
    书香云舍阅读 11,247评论 3 25
  • 第六章 组合数据类型 组合数据类型概述 计算机不仅对单个变量表示的数据进行处理,更多情况,计算机需要对一组数据进行...
    jplee阅读 9,214评论 0 1
  • 友谊是使青春丰富多彩的,清纯的生命的旋律,是无比美丽的青春赞歌。 以前,在常老师眼里,QQ就好像是一个犯罪的城...
    海贼迷惑阅读 1,326评论 0 4
  • 小lee_7cb7阅读 1,053评论 0 0