Python 彩蛋

精华 发表在    Python教程部落 11-11 09:08:52

1 2547 3

1,在Python里输入import this会发生什么呢?

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 之父呢。
做比不做好,但立马去做有时还不如不做。
如果实现很难说明,那它是个坏想法。
如果实现容易解释,那它有可能是个好想法。
命名空间是个绝妙的想法,让我们多多地使用它们吧!

Hello World

程序员们都熟悉 Hello World 的概念。在大多数情况下,它指的是使用该编程语言编写的最小程序,它会将“Hello World”打印到屏幕。这可能是在学习新的编程语言时首先要做的。

Python 有一个酷酷的隐藏的库,做了一些有点与众不同的事:

import __hello__

结果:

Hello World...

由 C++ 转换到 Python

'braces'库也是一个具有浓厚程序员风格的玩笑,它在其文档中提到,当在编写 Python 代码时使用这个库可提供使用 C++ 花括号的功能。但当你尝试使用它的时候,你将会看到社区对此的看法:

>>> from __future__ import braces
SyntaxError: not a chance

“反重力”对话

import antigravity

输入这行代码,会直接跳转到漫画网页 xkcd。漫画很有意思,一个人漂浮在天空中,他的一位朋友好奇的问他是怎么飞起来的。




登录或注册后发布评论