PyTips 0x00 - Python 之禅与 Pythonic

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?

    原文作者:Yushneng
    原文地址: https://segmentfault.com/a/1190000004566848
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞