Python - 概述
Python 是一种高级的、解释型的、交互式的面向对象脚本语言。Python 的设计目标是高度可读。它频繁使用英文关键词,而其他语言则使用标点符号,并且它比其他语言拥有更少的语法构造。
Python 是解释型的 − Python 由解释器在运行时处理。你无需在执行前编译程序。这类似于 PERL 和 PHP。
Python 是交互式的 − 你可以直接坐在 Python 提示符前与解释器交互来编写程序。
Python 是面向对象的 − Python 支持面向对象编程风格或技术,将代码封装在对象中。
Python 是初学者语言 − Python 是初学者程序员的绝佳语言,支持从简单文本处理到 WWW 浏览器再到游戏的各种应用开发。
Python 是一种开源的跨平台编程语言。它可在所有主要操作系统平台 Linux、Windows 和 Mac OS 上使用,并采用 Python Software Foundation License(与 GNU General Public License 兼容)。
为了引入新特性并保持可读性,开发了 Python 增强提案(PEP)流程。此流程允许任何人提交针对新特性、库或其他添加的 PEP。
Python 的设计哲学强调简单性、可读性和明确性。Python 以“开箱即用”(batteries included)的方法著称,其软件分发附带了全面的标准函数和模块库。
Python 的设计哲学记录在 Zen of Python 中。它包含十九条格言,例如 −
- 优美胜于丑陋
- 显式胜于隐式
- 简单胜于复杂
- 复杂胜于凌乱
要在 Python Shell 中获取完整的 Zen of Python 文档,请输入 import this −
>>>import this
这将输出以下 19 条格言 -
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 支持命令式、结构化以及面向对象编程方法论。它还提供了函数式编程的特性。
Pythonic 代码风格
Python 让你自由选择面向对象、过程式、函数式、面向切面甚至逻辑式编程方式。这些自由使 Python 成为编写干净优美代码的绝佳语言。
Pythonic 代码风格实际上更是一种设计哲学,建议编写如下代码:
- 干净
- 简单
- 优美
- 显式
- 可读
Python 的禅
Python 的禅关乎不仅能运行而且是 Pythonic 的代码。Pythonic 代码具有可读性、简洁性和可维护性。