遵循Python之禅:写出优雅简洁代码的秘诀

《Python 之禅》(The Zen of Python)是由 Tim Peters 编写的一组指导原则,它体现了 Python 语言的设计哲学和编程理念。你可以在 Python 的交互式解释器中输入import this来查看它。

import this

输出结果如下:

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之父。
做比不做好,但立马做有时不如不做。
难以解释的实现,不是好实现。
易于解释的实现,或许是好实现。
命名空间是个绝妙的想法,让我们多多使用吧!

 Python 之禅对编写 Python 代码提供了一种高层次的指导原则,鼓励开发者写出简洁、可读、易于维护的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值