Python (英語發音: /ˈpaɪθən/ ;英語發音: /ˈpaɪθɑːn/ ),是一种广泛使用的解释型 、高级 和通用 的编程语言 。Python支持多种编程范型,包括结构化、过程式、反射式、面向对象和函数式编程。它拥有动态类型系统 和垃圾回收 功能,能够自动管理内存使用,并且其本身拥有一个巨大而广泛的标准库。它的语言结构以及面向对象 的方法,旨在帮助程序员为小型的和大型的项目编写逻辑清晰的代码。
吉多·范罗苏姆 于1980年代后期开始研发Python,作为ABC语言 的后继者[ 18] ,它也可以被视为采用了叫做M-表达式 的中缀表示法 的一种LISP 方言[ 33] 。吉多·范罗苏姆 于1991年首次发布 Python 0.9.0[ 34] 。Python 2.0于2000 年发布并引入了新功能。Python 3.0于2008年发布,它是该语言的主要修订版,并非完全向后兼容 。Python 2于2020年随2.7.18版停止支持[ 35] 。
Python的设计哲学,强调代码的可读性 和简洁的语法,尤其是使用空格缩进 来划分代码块。相比於C语言 或Java ,Python让开发者能够用更少的代码表达想法。
Python解释器 本身几乎可以在所有的操作系统 中运行,它的官方直譯器 CPython 是用C语言 编写的。Python是一個由社群驱动的自由软件 ,目前由Python软件基金会 管理。Python是最受欢迎的编程语言之一[ 36] [ 37] [ 38] [ 39] 。
歷史
Python的創始人吉多·范羅蘇姆 ,在1982年至1995年间,参与了荷兰数学和计算机科学研究学会 多个项目的工作[ 40] 。1989年的聖誕節期間,他決心開發一個新的腳本解釋程式,作為ABC語言 的繼承者,并且用它替代Unix shell 和C语言 来进行系统管理[ 18] ,担负与Amoeba操作系统 [ 41] 之间的交互操作并进行例外处理 [ 10] 。他是BBC 電視劇《Monty Python的飛行馬戲團 》的爱好者,所以选取了Python作为这个编程语言的名字[ 42] 。范羅蘇姆作为Python的主要開發者,独自担负这个项目的发展决策者职责,直到2018年7月12日,他宣布从終身仁慈獨裁者 (BDFL)的职位上“永久休假”[ 43] [ 44] 。他在2019年1月至11月参与了第一届五人掌控委员会继续领导项目发展[ 45] [ 46] 。
在1991年2月,范羅蘇姆在alt.sources上发布了最初代码(标记为版本0.9.0)[ 1] ,这时就已经存在了带继承的 类 、例外处理 、函数 和核心类型 list
、dict
、str
等。在这个最初发行中就有了从Modula-3 引进的模块系统 [ 47] ,和例外处理 机制[ 10] 。在1994年1月,Python版本1.0发布[ 48] ,其主要新特征是由Amrit Prem提供的函数式编程工具lambda
、map
、filter
和reduce
[ 49] 。受Modula-3 启发,Python 1.1介入了缺省参数值 ,Python 1.3介入了关键字参数 。Python 1.4介入了对复数 的内建支持,还包含了采取名字修饰 的一种基本形式的数据隐藏 [ 50] 。
在2000年10月,Python 2.0發布,它从函数式编程 语言Haskell 中引进了列表推导式 ,并且支持了Unicode ,还向垃圾回收系统 增加了环 检测算法[ 51] 。Python 2.1支持了静态嵌套作用域 和闭包 [ 52] 。Python 2.2进行了重大革新,将Python中用C语言写成的类型 ,和用Python语言写成的类 ,统一成在同一个层级中,使得Python的对象模型成为纯粹而一致的对象模型[ 53] ;还介入了迭代器 [ 54] ,受CLU 和Icon 启发的生成器 [ 55] ,和描述器协议 [ 56] 。Python 2.3介入了从Dylan 引进的方法决定次序 [ 16] 。Python 2.4介入了集合 类型,和函数修饰器[ 57] 。Python 2.5介入了with
语句[ 58] ,并在官方实现中介入了抽象语法树 [ 59] 。
在2008年12月,Python 3.0發布,它对语言做了较大修订而不能完全后向兼容 [ 60] ,尽管提供了进行自动转换的2to3
实用工具,仍有大量现存代码不能移植,故而Python 2.7的产品寿命结束 延期至2020年元旦。Python 3.4介入了异步I/O 模块[ 61] 。Python 3.5介入了类型 提示[ 62] ,和采用async/await 语法的协程 [ 63] 。Python 3.8介入了赋值表达式[ 64] [ 65] 。
在2020年10月,Python 3.9介入了内建的针对容器 类的泛化别名 (types.GenericAlias
)类型 [ 66] ,并在官方实现中介入了新的语法解析器 [ 67] 。Python 3.10介入了结构式模式匹配 [ 68] ,和内建的联合类型 (types.UnionType
)[ 69] 。Python 3.11对官方实现进行了优化提速[ 70] 。Python 3.12介入了类型 参数 语法和type
语句[ 71] ,并废弃或移除了一些过时的模块和功能。Python 3.13介入了新的交互式解释器 ,并实验性的支持了在自由线程 模态下运行和即时编译器 [ 72] 。
每个版本首次发行后,享有2年的完全支持,随后是3年的安全支持。当前只有Python 3的稳定版本3.12与3.13正在被完全支持,但仍提供对3.9、3.10和3.11版本的安全性修正[ 73] 。
在2024年12月,活跃的Python核心开发者,选举Pablo Galindo Salgado、Barry Warsaw、Emily Morehouse、Gregory P. Smith和Donghee Na,为2025年度“掌控委员会”的五位成员来领导这个项目[ 74] 。
特徵與設計哲學
Python是多范型 编程语言。它完全支持结构化编程 和面向对象编程 ,还有很多特征支持函数式编程 和元编程 比如元对象 协议 (元类 和魔术方法[ 75] )。通过扩展还可以支持很多范型,包括面向方面编程 [ 76] 、契约式设计 [ 77] 和逻辑编程 [ 78] 。
Python使用动态类型 ,在内存管理 上采用的垃圾回收器 基于了引用计数 [ 79] ,并且结合了检测环 引用的分代垃圾回收 优化[ 80] 。它的特征还有动态名字解析 (后期绑定 ),即在程序执行期间绑定方法和变量的名字。
Python對遵循LISP 傳統的函数式编程 提供了有限的支持[ 81] ,它提供了 map
、filter
和reduce
函数[ 82] ;列表推导式 、字典 推导式 、集合 推导式 和生成器表达式 。標準庫中的模組functools
和itertools
,实现了从Haskell 和Standard ML 借鉴来的函數式工具[ 83] 。
Python的設計理念是“優雅”、“明確”、“簡單”,它的一些重要準則被合稱為「Python之禅 」。在Python解釋器内運行import this
可以獲得完整的列表,下面举出其中首要:
優美优于丑陋。明瞭优于隐晦。
简单优于复杂。复杂优于凌乱。
扁平优于嵌套。稀疏优于稠密。
可读性很重要。
Python開發者的方法论是“用一種方法,最好是只有一種方法來做一件事”,显著不同于以Perl 语言为代表的“不止一种方法去做一件事 ”風格。Python開發者在設計語言時,如果面臨多種選擇,一般會選擇明確没有或者很少有歧義的語法。
范羅蘇姆认为ABC語言非常優美和强大,它没有取得成功的原因是不開放造成的[ 84] ,故而将Python本身設計為可擴充的[ 85] 。Python並不把所有的特性和功能都集成到語言核心,而是提供了豐富的API 和工具,以便程式設計師能够輕鬆地使用Python、C 语言、Cython 來編寫擴充模組。Python还可以通过外界函数接口 如标准库中的ctypes等,来提供C语言兼容数据类型 ,并访问动态链接库 或共享库 中的函数[ 86] ,从而对用其他語言編寫的程式進行集成和封裝。
在Python的官方实现CPython 中,一般避開不成熟的或者對非重要部位的加快運行速度的優化。在某些對運行速度要求很高的情況,可以使用具备JIT 技术的Python实现或安装JIT扩展模块[ 87] 。
語法和语义
Python為了讓程式碼具備高度的可閱讀性,在設計時盡量使用了其它語言常用的符號和英文單字。
行结构
Python支持使用反斜杠 作为行接续符,将多个物理行合成为一个逻辑行[ 88] 。在圆括号 、方括号 或花括号 之中的表达式,可以分裂跨越多于一个物理行而不使用反斜杠,这被称为“隐式行接续”[ 88] 。注释 开始于并非字符串文字 一部份的一个井号 #
,并结束于物理行结尾;注释标示逻辑行的结束,除非已受制于隐式行接续规则;注释在语法上被忽略[ 89] 。
简单语句包含在一个单一的逻辑行之内,Python支持使用分号 作为分隔符,将多个简单语句合并入语法意义上的一行之中[ 90] 。
縮排
Python语法中的复合语句,包含(成组的)其他语句;它们以某种方式影响或控制这些其他语句的执行。Python的复合语句包含一个或多个子句(clause),子句构成自一个头部(header)和一个套件(suite)。特定复合语句的子句头部都在同样的缩排 层级上,每个子句头部开始于一个唯一标识关键字 ,并结束于一个冒号 。套件是这个子句所控制的一组语句,套件有两种形式,可以是与头部在同一行上的一个或多个由分号分隔的简单语句,它们跟随在这个头部的冒号之后;或者是在后续诸行上的一个或多个缩排的语句,只有这种套件形式可以包含嵌套的复合语句[ 91] 。
Python語言遵循越位規則 ,利用縮排 来形成语句套件,即语法意义上的块 。连续诸行的缩排层级,被用来生成语法解析器 才能见到的INDENT
和DEDENT
记号[ 92] ,二者的作用相当于C 语言家族的花括号 ,或Pascal 语言家族的关键字 begin
和end
。增加縮排就生成INDENT
记号,減少縮排就生成DEDENT
记号。根據PEP 8的規定[ 93] ,使用4個空格 來表示每級縮排 。[ a]
tab 字符(从左至右)被替代为1至8个空格,使得直到tab之前的诸字符加上这些替代空格的字符总数,是8的倍数(这意图同于Unix所用规则)。前导于第一个非空白字符 的空格的总数,确定了这一行的缩排层级。缩排所用诸字符,不能使用反斜杠来拆分成多个物理行;直到第一个反斜杠之前的空白确定缩排层级。如果源代码文件混合了tab和空格,并且在这种方式下缩排的意义依赖于一个tab相当于多少个空格,则这种缩排因不一致性而被报错并拒绝[ 94] 。
关键字
Python有如下35个关键字 ;它们不能用作标识符[ 95] :
and
as
assert
async
await
break
class
continue
def
del
elif
else
except
False
finally
for
from
global
if
import
in
is
lambda
None
nonlocal
True
try
while
with
yield
内建常量True
、False
和None
于Python版本3.0中成为关键字,关键字nonlocal
介入于版本3.0[ 96] ,关键字async
和await
介入于版本3.5[ 97] ,并在版本3.7中成为正式关键字[ 98] 。
在Python中,将只在特定上下文中保留的标识符,称为“软关键字”[ 99] :
match
、case
和通配符 _
,介入于版本3.10,它们在与模式匹配 语句有关的上下文中,可以在语法上充当关键字;但是这种区分只在语法解析器层次进行,并非在词法分析记号化层次。
type
,介入于版本3.12,它用在type
语句之中。
标识符
标识符就是名字,在ASCII 范围内(U+0001..U+007F),可用于标识符的字符为:大写字母A
至Z
和小写字母a
至z
,下划线_
以及数字0
至9
,但首字不可以用数字。如下命名约定 [ 100] ,是为“保留标识符类”[ 101] :
_spam
(单下划线开头):弱“内部使用”标识。对于from M import *
,将不导入所有以下划线开头的对象。
spam_
(单下划线结尾):为了避免与python关键字的命名冲突。
__spam
(双下划线开头):在命名一个类特性 的时候,采用名字修饰 ,比如在类SpamEggs
内,__spam
将变成_SpamEggs__spam
[ 102] 。
__spam__
(双下划线开头双下划线结尾):指那些包含在用户控制的命名空间 中的“魔术”方法或特性 ,比如__delattr__
、__dir__
、__doc__
、__getattribute__
、__init__
、__new__
、__repr__
、__setattr__
、__sizeof__
等。建议永远不要将这样的命名方式应用于自己的变量或函数。
在Python文献中经常使用的元语法变量 是spam和eggs 而非传统的foo和bar [ 102] 。
語句
Python的语句 包括简单语句:
赋值 语句,采用的中缀 记号是等号=
。赋值语句被用来将名字绑定(含重新绑定)到值,以及用来修改可变对象 的特性 或项目。
Python还支持增广赋值 语句[ 103] ,将一个二元运算和一个赋值语句合并成一个单一语句,例如x += 1
。
Python支持“序列解包”(sequence unpacking)[ 104] :在等号左侧可以是一个表达式列表,其中每个表示式都可求值成能被赋值的东西(变量、可写特性等);在等号右侧相应的是一个“可迭代”对象,它在被迭代时产生的值的数量,同于左手侧可写表达式的数量;赋值语句对这个对象进行迭代,将产生的每个值分别赋值给左侧对应的可赋值者。在等号右侧直接包装出序列解包所要求的元组,就形成了并行赋值 ,可以同时给多个变量赋值,还可以交换两个变量的值。[ b]
表达式 语句,用来交互式的计算并写出一个值,或者用来调用一个过程(即返回无含义结果的函数),在Python中过程返回值None
。
global
语句,是在整个当前代码块中成立的声明,它意味着随后列出的标识符被解释为全局变量。
nonlocal
语句,导致随后列出的标识符,提及在除了全局作用域之外的最近包围作用域中的先前绑定变量。
type
语句,介入于版本3.12,声明作为类型别名类型 (typing.TypeAliasType
)的实例的一个类型别名。
pass
語句,充当NOP ,表示此行為空,不執行任何操作。
assert
語句,用於程式調適階段時測試執行條件是否滿足。
continue
语句,越过这次迭代并继续进行下个项目。
break
语句,从循环中跳出。
return
语句,用来从函数返回值。当函数执行到return
语句时,它会停止执行并将指定的值返回给调用者。
raise
语句,抛出一个例外。
yield
语句,使用它从一个生成器 中返回一个值。在版本2.5之前,信息只能单向的从生成器传递出来。[ c]
自此版本2.5,重定义yield
为表达式[ 105] ,通过它的返回值将信息传递进入生成器函数中[ 106] ,从而能够支持协程 功能[ 106] 。[ d] yield
语句在语义上等价于加圆括号的yield
表达式[ 107] 。
自从版本3.3,提供了yield from
语句,含有这个语句的“委托生成器”将其部份运算委托给另一个“子生成器”,将传入信息递送给它并直接回传它产生的值[ 108] 。版本3.4在异步I/O 框架中扩展了基于生成器的协程[ 109] ,这个扩展自从Python 3.8已经被弃用[ 110] 。
自从版本3.6,介入了生成async for
语句所用的异步迭代器的异步生成器[ 111] 。
import
语句,导入一个模块或包,它组合了两种操作,查找指名的模块,接着将找到的结果绑定到在局部作用域中的名字。导入语句有三种形式(下述语句样本中的方括号表示其中内容为可选的):
import 模块名字 [as 别名 ]
,找到一个模块,装载它,如果有需要的话初始化它;在这个导入语句出现的作用域的局部名字空间 中,定义一个名字或一些名字[ 112] 。
from 模块名字 import 定义1 [as 别名1 ], 定义2 [as 别名2 ], ...
,找到、装载、必需时初始化一个模块;接着在局部名字空间 中,增加到找到指名特性的引用[ 112] 。
from 模块名字 import *
,在导入语句出现的作用域的局部名字空间中,绑定模块中定义的所有公开的名字[ 113] 。
del
语句,递归的进行删除。
复合语句:
if
語句,當條件成立時執行語句套件。它經常包含elif
、else
子句。
while
語句,當條件為真時,重複執行語句套件。
for
語句,遍历列表、字符串、字典、集合等迭代器 ,依次處理迭代器中的每個元素。
match
语句,用于模式匹配 。
try
語句,它经常包含except
、else
、finally
子句,處理在程式執行中出现的異常情況。Python支持并广泛使用例外处理 ,作为检测错误状况和程序中其他“例外”事件的方式,并提倡在可能出现错误状况的任何时候都使用例外。习惯上访问一个文件或资源不在使用之前进行测试,而是先行尝试使用它,再捕获访问被拒绝引发的例外。[ e] Python 3.11介入了except*
子句[ 114] 。
with
語句,把一块代码包裹在一个上下文管理器之内。它允许了资源获取即初始化 (RAII)式行为,可替代常见的try
/finally
惯用法。Python使用with
语句处理资源[ 115] ,在进入一个作用域的时候调用一个函数,而在离开它的时候调用另一个函数,例如:在一块代码执行之前获取一个锁 ,并且在此后释放这个锁;或事先打开一个文件 ,并且事后关闭它。[ f]
class
語句,是定义类 的可执行语句。类的继承列表给出基础类列表,没有继承列表的类,缺省继承基础类object
。类的套件接着在新的执行框架(frame)中执行,它使用新建的局部名字空间 和原来的全局名字空间。当这个类套件完成执行之时,丢弃它的执行框架并保存它的局部名字空间。一个类对象接着被创建,其基础类采用继承列表,其特性字典采用保存的局部名字空间。类名字接着在原来的局部名字空间中,被绑定到这个类对象。
def
語句,是定義函數 和方法 的可执行语句。它的执行在当前局部名字空间 中,将函数名字绑定到一个函数对象(对函数的可执行代码的包装器)。这个函数对象包含到当前全局名字空间的引用,作为调用这个函数时使用的全局名字空间。
async def
语句,用于协程 函数定义。await
表达式、async for
语句和async with
语句,只能用在协程函数的主体中。[ g]
块与模块
Python程序构造自代码块 。块是作为一个单元执行的Python程序文本,模块、函数主体和类定义都是块。交互式键入的每个命令、脚本文件和脚本命令都是代码块。传递给内建函数eval()
和exec()
执行的字符串是代码块。
代码块在执行框架(frame)中执行。框架包含一些用于调试的管理信息,并确定在这个代码块执行完成后,执行在何处以及如何继续。名字空间 是存储变量的地方,它被实现为字典。有局部名字空间、全局空间即包含此代码块的那个模块的名字空间,和内建名字空间即模块builtins
的名字空间;对象的方法是定义在类主体内的函数,它有着嵌套的名字空间。名字空间通过防止命名冲突而支持了模块性,还通过明晰了哪个模块实现了哪个函数而增进可读性和可维护性。
模块 是包含Python定义和语句的一个文件,这个文件名字是模块名字附加上后缀.py
;在一个模块中,模块的名字(作为字符串)可获得为全局变量__name__
的值[ 113] 。包(package)是可以包含子模块或递归性的子包的模块。包在技术上是具有__path__
特性的Python模块。可以将包视为文件系统上的目录,而将模块视为这种目录中的文件,但是包和模块不必然源自文件系统[ 116] 。
完整的Python程序,在一个极小初始化的环境中执行:所有内建和标准模块均可获得,但除了sys
(各种系统服务)、builtins
(内建函数、例外 和None
)和__main__
之外都未被初始化。__main__
用来为完整程序的执行提供局部和全局名字。当解释器被调用在交互模态下的时候,它一次一个的读取并执行语句;初始环境同于完整程序,每个语句都在__main__
的名字空间中执行。
顶层代码是启动运行的首个用户指定Python模块。__main__
是顶层代码运行所在的环境。从命令行使用-m
参数,作为顶层脚本运行的模块(作为模块__main__
)是代码块。此时__name__
变量被设置为"__main__"
,籍此可在这个模块中增加直接运行时候执行的代码[ 113] [ 117] 。
名字是通用的引用持有者,它不关联于一个固定的数据类型 ,但是,一个名字在给定时间,总是被绑定 到有一个类型的某个对象上,这就是动态类型 的特征。名字的存储位置不“包含”所指示的值,一个共同的值可以赋值给多个名字,一个名字在任何时候,都可以重新绑定到各种不同类型的对象上,包括字符串、过程、具有数据和方法的复杂对象等。如果一个名字绑定在一个块 中,它是这个块的局部变量 ,除非被声明为nonlocal
或global
。如果一个名字绑定在模块层次,它是全局变量 。模块代码块的变量,既是局部的也是全局的。如果一个变量使用在一个代码块中,却不定义在这里,它是自由变量 [ 118] 。
内建vars()
函数,返回一个模块、类、实例或任何具有字典特性__dict__
的对象的字典特性。内建globals()
函数,返回实现当前模块的名字空间的一个字典。内建locals()
函数,更新并返回表示当前局部符号表的一个字典。在函数块中而非类块中调用locals()
之时,它返回自由变量。在模块层级上,locals()
和globals()
返回同一个字典。内建dir()
函数,在无参数时,返回在当前局部作用域内的名字列表;在有一个参数时,尝试返回这个对象的有效特性的列表。[ 119]
在Python中赋值所进行的操作,是将一个名字绑定 为到一个分立的动态分配的对象 的一个引用 。除了在块中出现的每个赋值语句或导入语句之外,下列构造也绑定名字:给函数的形式参数、类定义、函数定义、赋值表达式、在for
语句头部中和各种as
关键字之后的标识符目标(target),as
关键字出现在import
语句、with
语句、except
子句、except*
子句和结构式模式匹配的as
模式之中。
作用域 定义一个名字在一个块 中的可见性。如果一个局部变量被定义在一个块中,它的作用域包括这个块。如果这个定义出现在一个函数块中,作用域扩展到在所界定作用域内包含的任何块,除非所包含的块为这个名字介入了不同的绑定。当一个名字在一个代码块之中使用,它采用最近包围作用域来解析。对一个代码块可见的所有这种作用域的集合,叫做这个这个块的“环境”[ 118] 。
如果一个名字绑定在一个块中,并且在其中于绑定之前就被使用,会导致一个错误。[ h]
如果global
语句出现在一个块之中,在这个语句中指定的所有名字,提及在顶层名字空间中这些名字的绑定。名字在顶层名字空间解析,首先查找全局名字空间 ,未果查找内建名字空间。global
语句与在同一个块中的名字绑定运算有同样的作用域。如果一个自由变量的最近包围作用域包含针对它的global
语句,这个自由变量被当作全局的[ 118] 。[ i]
当一个函数或类的定义被嵌套到其他函数的定义之内,它的非局部作用域就是这个包围函数的局部作用域。nonlocal
语句导致其列出的标识符,提及在非局部作用域内先前绑定的名字(即非局部变量 )[ 118] 。[ j]
表达式
Python中很多表达式 与C 语言和java 类似,而另一些则与之不同。
在Python中,有如下必须用于整数的位 运算:&
与(AND),|
或(OR),~
非(NOT),^
异或(XOR),>>
右移, <<
左移。
在Python中,有如下比较 运算:大于>
,小于<
,等于==
,不等于!=
,小于等于<=
,大于等于 >=
。==
按值比较。Python的is
、is not
算符可以用来比较对象的同一性(按引用比较),也就是比较两个变量是否引用了同一个对象。而in
、not in
用于判断一个对象是否属于另外一个对象。在Python中,比较是可以链接起来的[ 122] ,比如a < b < c
。
Python使用and
、or
、not
表示逻辑 运算与、或、非,不采用C 语言和Java 中所用的符号&&
、||
、!
。
在Python中,由逗号,
分隔的一组表达式,叫做表达式列表。Python为构造列表、字典或集合,提供了叫做“展示”(display)的特殊语法,它们每个都有两种方式:包容内容要么显式列举出来;要么通过一组循环和过滤指令计算而来,这叫做“推导式”。列表展示是包围在方括号中的可以为空的一系列表达式,一个例子列表可写为[1,2,3]
。字典展示是包围在花括号中的可能为空的一系列的键/数据项对。集合展示用花括号来指示,与字典展示的区别是缺少分隔键与值的分号[ 123] 。自从Python 3.5,增加了在表达式列表中的“可迭代解包”*
,和在字典展示中的“字典解包”**
[ 124] 。[ k]
在Python中,加圆括号形式(parenthesized form),是包围在圆括号中的一个可选表达式列表。加圆括号的表达式列表产生的东西,就是这个表达式列表所产生的:如果这个列表包含至少一个逗号,它产生一个元组;否则它产生构成这个表达式列表的那个单一表达式[ 126] 。一个例子元组可写为(1,2,3)
。元组不是圆括号形成的,而是使用逗号形成的,在没有歧义的情况下,元组的圆括号是可选的。空的圆括号对产生空元组对象。使用序列串接算符+
来串接二个元组,产生包含给定元组二者的元素的一个新元组。
Python支持在序列对象(比如字符串、元组或列表)上的下标 (subscription)表达式:a[索引 ]
,和分片 表达式:a[开始 :停止 ]
或a[开始 :停止 :步长 ]
。下标索引是基于零 的,负数是相对于结尾的。分片范围自从“开始”索引,直到但不包括“停止”索引。分片的第三个参数叫做“步长”(step)或“间隔”(stride),允许元素被跳过和用负数指示反向。分片索引可以省略,例如a[:]
,这返回整个列表的一个复本。[ o] 分片的每个元素都是浅层复制 的。
Python的条件表达式 表示为x if c else y
。意思是当c
为真时,表达式的值为x
,否则表达式的值为y
。 在运算元的次序上不同于很多其他语言中常见的c ? x : y
。
Python的匿名函数 实现为lambda
表达式。匿名函数体只能是一个表达式。[ p]
自从Python 3.8,介入了赋值表达式,其记号是:=
[ 64] 。它将一个表达式赋值给一个标识符,同时还返回这个表达式的值。赋值表达式在用作子表达式,即位于分片表达式、条件表达式、lambda
表达式、关键字参数中的表达式和推导式中的if
表达式之中,以及在assert
和with
语句之中的时候,必须围绕着圆括号。在它们可以使用的所有其他地方,包括在if
和while
语句之中,都不要求圆括号[ 127] 。
Python中运算符 具有优先级 ,下表中的运算符按照从最高到最低的次序列出。在相同单元格中运算符具有相同的优先级,它们从左至右结合,除了指数表达式和条件表达式从右至左结合之外[ 128] :
运算符
描述
(表达式 ...)
,[表达式 ...]
,{键 : 值 ...}
,{表达式 ...}
加圆括号表达式,列表展示,字典展示,集合展示
x[索引 ]
,x[索引 :索引 ]
,x(参数 ...)
,x.特性
下标,分片,调用,特性引用
await x
await
表达式
**
指数
+x
,-x
,~x
取原数,相反数,逐位NOT
*
,@
,/
,//
,%
乘法,矩阵乘法,除法,下取整除法,余数
+
,-
加法和减法
<<
,>>
移位
&
逐位AND
^
逐位XOR
|
逐位OR
in
,not in
,is
,is not
,<
,<=
,>
,>=
,!=
,==
包含成员关系测试,同一测试,各种比较
not x
布尔NOT
and
布尔AND
or
布尔OR
if – else
条件表达式
lambda
lambda表达式
:=
赋值表达式
Python提供了序列串接算符+
和序列倍增算符*
[ 129] 。自从Python 3.9,介入了字典归并算符|
和字典更新算符|=
[ 130] 。
Python的文本序列类型,包括字符串str
和字节序列bytes
与bytearray
。字符串文字 有多种写法,字符串对象有一个内建格式算符%
:
字节 文字 总是前缀上一个b
或B
,它产生bytes
类型的实例。它们只可以包含ASCII 字符,具有128或更大数值的字节必须通过转义来表达。
Python允许多个毗邻的字符串文字或字节文字(它们以空白分界并可以使用不同的引述约定),在编译时间于语法层面上串接起来。要在运行时间串接字符串,必须使用序列串接算符+
[ 131] 。[ s]
Python中的“字符串格式”算符%
,在功能上类同于C 语言中的printf
格式化字符串 [ 132] ,例如"spam=%s eggs=%04d" % ("blah", 2)
,求值为'spam=blah eggs=0002'
。自从Python 3.0,str
类提供了可供替代的format()
方法[ 133] ,例如"spam={0} eggs={1:04d}".format("blah", 2)
。在Python 3.6中,提供了“格式化字符串文字”或称为“f字符串”,它向字符串文字前缀上f
或F
[ 134] ,这是一种字符串插值 [ 135] ,例如x="blah"; y=2; f'spam={x} eggs={y:04d}'
。[ t]
在Python中,在表达式和语句之间的区别是严格强制性的,这对比于语言如Common Lisp 、Scheme 或Ruby 。故而Python中个别构造存在功能重复,比如:列表推导式 相当for
循环;条件 表达式相当if
语句;内建函数eval()
相当exec()
,前者用于表达式,后者用于语句。
语句不能成为表达式的一部份,由于列表和其他推导式或lambda表达式 ,都是表达式,也就不能包含语句。这个限制的一个示例:赋值语句比如a = 1
,不能用作条件语句的条件判断表达式的一部份;这能够避免C语言编程中的一个常见错误,即在条件判断时把等于算符==
误写为赋值算符=
,这不是预期代码却在语法上有效而能通过编译器检查,在Python中这会导致一个语法错误。
函数
Python的函数支持递归 和闭包 [ u] ,及其他头等函数 特征,但不支持函数重载 。Python的函数作为头等对象 ,具有和普通对象平等的地位。Python官方实现不提供尾调用 优化或头等续体 ,吉多·范罗苏姆曾声称永远都不会加以支持[ 136] ,但有第三方库支持弹跳床 [ 137] 。
Python可以在函数定义时,于形式参数序列中,指定形式参数缺省值 ,即以param=value
样式进行一次性初始化。形式参数在初始化之后,保持既有绑定;函数的后续调用,可继续对它进行访问或变更。[ v] 为有缺省值的形式参数提供实际参数,在函数调用时是可选的。
Python的函数实际参数与形式参数之间的结合,是传递“对象引用”,函数在被调用的时候,给函数调用的实际参数,被介入到一个局部符号表中,实际参数使用传值调用来传递,而这个值总是对象引用,而非这个对象的值[ 138] 。如果形式参数绑定到一个可变的对象,则通过形式参数对此对象内容的修改,在函数外也是可见的。如果形式参数绑定到一个不可变的对象,则通过形式参数是不能修改此对象内容,但可以把形式参数重新绑定到其它对象上,这并不影响函数外的对象的值。[ w]
Python支持位置实际参数和关键字实际参数。函数调用时,实际参数可以如同C语言那样,按照位置与形式参数匹配;也可以采用命名参数 或称为关键字实际参数,即kwarg=value
样式的实际参数。使用不对应实际参数的特殊形式参数/
和*
,可以将参数序列分为三部份:唯位置参数[ 139] 、可位置可关键字参数和唯关键字参数。有缺省值的形式参数之后,不能跟随无缺省值的可位置形式参数。[ x] 在一个函数调用的实际参数序列中,关键字实际参数必须出现在位置实际参数之后。
在位置和关键字形式参数序列末尾,可以分别有*args
或**kwargs
这样的形式参数,它们对应于在函数调用时提供的,超出形式参数序列规定而无所对应的多个实际参数;在形式参数名字前加一个*
号,该形式参数args
是tuple
类型,对应可变数目的位置实际参数;在形式参数名字前加**
号,该形式参数kwargs
是dict
类型,对应可变数目的关键字实际参数。[ y] 如果位置实际参数已经在一个序列类型如列表或元组的对象中,在引用它的变量前加一个*
号传递给函数,则其中所有元素解包为多个位置实际参数;如果关键字实际参数在字典中,则加**
号来传递给函数。
修饰器(decorator)可用来修改一个函数、方法或类定义的任何可调用Python对象。将已定义的原来对象传递给修饰器,它返回一个修改后的对象,接着把它绑定到在定义中那个名字。Python修饰器部份受到Java注解 的影响,而有类似的语法;修饰器语法是纯粹的语法糖 ,使用@
作为关键字形成修饰符。修饰器是一种形式的元编程 ,它们增强它们所修饰的函数或方法的行动。[ z]
多个修饰器可以链接起来,通过在毗连的行上放置多个修饰符,或者使用中间变量。[ aa]
函数修饰器的正规用法包括:用来建立类方法 或静态方法 [ 57] 、设置先决条件 和后置条件 、实现多方法 、增加函数特性、跟踪 、同步 [ 140] ;此外更远大的用法包括:尾调用消除 、记忆化 [ 141] 。
为了增强代码的可读性,可以在函数后书写“文档字符串”(简称docstrings),用于解释函数的作用、参数的类型与意义、返回值类型与取值范围等。可以使用内置函数help()
,打印出函数的使用帮助。[ ab] 自从Python 3.0,函数可以对参数与返回值增加类型标注[ 142] 。此特性可方便对源代码进行更深入的分析。[ ac] 自从Python 3.5,开始支持类型提示[ 143] 。
对象及其方法
Python支持大多数面向对象编程 技术。在Python中所有东西都是对象,包括类 、函数、数和模块 。它允许多态性 ,不只是在类层级 之内,而且通过采用鸭子类型 的方式[ 5] 。任何对象 可以用于任何类型 ,只要它有适当的方法 和特性 (attribute)就能工作。Python天然支持类 的继承 包括多重继承 ,为此采用C3线性化 或方法决定次序(MRO)算法,还支持混入 。Python支持元类 [ 144] ,自从Python 3.6,提供了定制类创建的简单机制[ 145] 。
Python使用名字修饰 ,有限的支持私有变量。对象的(可写)特性可以被提取为一个字典[ 146] 。在Python中,不强制使用访问子 与变异子 方法,来访问数据成员 的面向对象编程信条。就像Python提供函数式编程构造,但不尝试要求参照透明性 一样,它提供对象系统,但不要求面向对象编程行为。
对象 的方法 ,是附属于这个对象的类 的函数。对于正常的方法和函数,语法instance . method ( arguments )
,是Class . method ( instance , arguments )
的语法糖 。Python的方法有显式的self
形式参数,用来访问实例数据 ;这借鉴自Modula-3 ,对立于隐式的self
或this
关键字,它们用在其他一些面向对象编程语言,比如C++ 、Java 、Objective-C 或Ruby 之中[ 147] 。在Python中,self
可以被看作是一个习惯用法,它可以被换为任何其它合法的参数名。[ ad]
Python提供了super()
内建函数,在一个类的方法中调用此函数返回一个代理(proxy)对象,它将其方法调用委托 给这个类的父类或兄弟类[ 148] ,当一个子类 的方法覆盖 了超类 方法的时候,可通过调用super().method
,来调用与子类的self.method
方法同名超类方法。[ ae]
Python支持一些以__
开始和结束的特殊方法名,它们用于实现运算符重载 ,以及实现多种特殊功能[ 75] 。在Python中,可以通过定义特殊方法来重载运算符,比如在一个类上定义__add__()
,将允许在这个类的实例上使用+
算符。
在Python中,定义了一个或多个特殊方法__get__()
、__set__()
、__delete__()
的类,可以用作描述器(descriptor)[ 149] 。建立一个描述器的实例,作为另一个类的一个类成员,使得这个实例成为此另一个类的属性 (property)。使用与特性 (attribute)访问相同的语法,访问一个实例对象中的这个成员属性。[ af]
Python允许通过使用@classmethod
和@staticmethod
修饰符,来分别建立类方法 和静态方法 [ 57] 。给类方法的第一个实际参数,是对类对象的引用,而非对实例的self
引用。静态方法没有特定的第一个实际参数,实例或类对象,都不固定的传递给静态方法。[ ag]
Python的property
内建函数,将一个类中特殊定义的访问一个特性的那些方法,包装成的这个类的一个属性[ 150] 。[ ah]
类型
Python 3的标准类型层级[ 151]
Python使用鸭子类型 ,并拥有有类型的对象,和无类型的变量名字。在编译期 不检查类型约束,而宁愿在一个对象上的操作出现可能的失败,表现出这个给定对象不具有适合的类型。尽管是动态类型 系统,Python却是强类型 的,禁止没有明确定义的操作(比如加一个数到一个字符串),而不是默默的去尝试转换使其有意义。Python支持广泛的类型和类的内省 。类型是type
的实例,可以被读取和比较。
Python有着范围广泛的基本数据类型。同时具备常规的整数和浮点 算术,它透明的支持任意精度算术 、复数 和十进制浮点数 。Python支持种类繁多的字符串操作。在Python中,字符串是不可变 的,所以在其他编程语言中可能就地改变字符串的字符串操作,比如字符替换,在Python中返回新的字符串。
Python有一个非常有用特征,就是搜集 (或称容器 )类型的概念。一般的说,搜集是以一种易于引用或索引的方式,包含其他对象的对象。搜集有二种基本形式:序列和映射。Python对建立容器类型的对象有着语法上的支持。[ ai] Python还提供了广泛的搜集操纵能力,比如内建的包含元素检查和通用迭代协议 。
有次序的序列类型是列表(动态数组 )、元组 和字符串。所有序列类型都是位置索引的(从0
到长度 −1
),并且除了字符串,都可以包含任意类型的对象,在同一个序列中包括多种类型的对象。字符串和元组是不可变的,使得它们成为字典的键的完美候选者。在另一方面,列表是可变的,元素可以被插入、删除、修改、添加或就地 排序。
在另一方面,映射是以“字典”形式实现的无次序的类型,它将一组不可变 的键,映射到相应的元素上(非常像数学函数)。在字典中的键,必须是不可变的Python类型,比如整数或字符串,因为在底层它们是通过散列函数 实现的。字典还是语言内部的中心,因为它们居于所有Python对象和类的核心:在变量名字(字符串)和这个名字所引用的值之间的映射,就存储为字典,而这些字典可以通过对象的__dict__
特性直接访问。
集合 搜集类型,在版本2.4中被增加入语言核心。集合是无索引、无次序的搜集,它包含唯一性的不可变对象作为元素,并且实现了集合论 运算,比如并集 |
、交集 &
、相对补集 -
、对称差 ^
,和子集 测试<=
、真子集 测试<
、超集 测试>=
、真超集 测试>
。有二种类型的集合:可变的set
和不可变的frozenset
。
Python允许编程者使用类 ,定义自己的类型[ 53] ,类是在面向对象编程 中最经常使用的。类的新实例 ,是通过调用这个类的构造器 而创建的,而类都是元类 type
的实例,type
是type
元类自身的实例,这允许了元编程 和反射 。[ aj]
在版本3.0之前,Python有两种类:旧式的和新式的[ 152] 。二种样式的语法是一样的,不同在于是否直接或间接的继承自类object
,所有新式类都从object
继承,并且是type
的实例。在Python 2系列2.2以上,二种类都可以使用[ 53] 。在Python 3.0中淘汰了旧式类。
长期规划是支持渐进类型 [ 7] ,并且自从Python 3.5,语言的语法允许指定静态类型,但在缺省实现CPython中不检查它们[ 143] 。有叫做“mypy”的可选的静态类型检查器,支持编译期类型检查[ 153] 。
Python 3内建类型小结
类型
可变性
描述
语法例子
bool
不可变
布尔值 ,有表示值False
和True
的两个对象。作为整数类型numbers.Integral
的子类型,它们在几乎所有上下文中,表现得如同0
和1
,除了在转换成字符串时转换为"False"
和"True"
之外。
True
False
int
不可变
整数 ,其大小在理论上无限制,实际上受限于内存[ 154] 。
42
float
不可变
双精度 浮点数 ,确切精度依赖于机器。一般实现为IEEE 754 标准binary64 浮点数,它有53个二进制 有效 数位 精度[ 155] 。
1.414
complex
不可变
複數 ,即分别表示实部与虚部的两个双精度浮点数的有序对 。
3 + 2.7 j
range
不可变
数的序列,通常用在for
循环中指定循环次数[ 156] 。
range ( 1 , 10 )
range ( 10 , - 5 , - 2 )
str
不可变
字符串 ,即Unicode 代码点 序列。字符串中的代码点都在范围U+0000..U+10FFFF
之内。Python没有char
类型,这些代码点都表示为长度为1
的字符串对象。
'Wikipedia'
"Wikipedia"
"""Spanning
multiple lines"""
bytes
不可变
字节 序列,其项目是8位字节,用范围0 <= x < 256
的整数表示。
b 'Some ASCII'
b "Some ASCII"
bytes ([ 0x53 , 0x74 , 0x72 ])
bytearray
可变
bytearray ( b 'Some ASCII' )
bytearray ( b "Some ASCII" )
bytearray ([ 0x53 , 0x74 , 0x72 ])
list
可变
列表 ,可以包含任意的Python对象。
[ 4.0 , 'string' , True ]
[]
tuple
不可变
元组 ,可以包含任意的Python对象。只有一个项目的元组,可以通过向表达式后缀一个逗号来形成。
( 4.0 , 'string' , True )
( 'single element' ,)
()
dict
可变
键-值 对 的关联数组 (常称为字典),即由任意索引集合 来索引的对象的有限集合 。不可接受为键的值,是列表或字典,或按值而非对象同一性比较的其他可变类型的值,其散列 值不能保持恒定。
{ 'key1' : 1.0 , 3 : False }
{}
set
可变
无序有限集合 ,包含唯一性的不可变的对象,它们不能用任何下标来索引。
{ 4.0 , 'string' , True }
set ()
frozenset
不可变
frozenset ([ 4.0 , 'string' , True ])
types.EllipsisType
不可变
这个类型有一个单一对象作为值,它通过文字...
或内建名字Ellipsis
来访问,它的真值 为真。它用于NumPy 多维阵列索引[ 157] 。
...
Ellipsis
types.NoneType
不可变
这个类型有叫做None
的一个单一对象Null
作为值[ 158] ,它被用来指示值的缺席,比如不返回任何东西的函数返回它,它的真值 为假。
None
types.NotImplementedType
不可变
这个类型有一个单一对象NotImplemented
作为值。数值方法和细化比较方法,在它们仍未对提供的运算元实现这个运算之时,返回这个值。它不应该在布尔值 上下文中求值。
NotImplemented
除了各种数据类型,Python解释器还内建了很多其他类型,包括可调用类型:用户定义函数、实例方法、生成器函数、协程函数、异步生成器函数、内建函数、内建方法、类、类方法;模块,定制类,类实例,I/O对象(也叫做文件对象),和暴露给用户的一些内部类型:代码对象、框架对象、溯回对象、切片对象、静态方法对象、类方法对象。
算术运算
Python的算术运算,使用平常的符号+
、-
、*
、/
和模除 %
。它还有下取整 除法算符//
、指数 算符**
和矩阵乘法 算符@
[ 120] 。二元运算先将两运算元转为共同类型,加法、减法、乘法、下取整除法和指数运算的结果也采用此类型,比如5**3 == 125
而9**0.5 == 3.0
。这些算符就像在传统数学中一样运算,具有同样的优先级规则 ,中缀 算符+
、-
,还可以分别表示取原数和取相反数 的一元 算符。
被称为“真除法”的/
的表现,随着版本不同而有着显著变化[ 159] 。自Python 3.0以来,/
总是产生浮点数结果,包括两个整数相除,比如5/2 == 2.5
;而在下取整除法//
中,两个整数相除产生整数,比如7//3 == 2
而7.5//3 == 2.0
。
下取整除法//
的修约 是朝向负无穷 的。向下取整 能增加一致性[ 160] ,例如这意味着等式( a + n ) // n == a // n + 1
总是为真。模除 %
所得出的余数 的符号同于除数 ,比如-4%3 == 2
而4%-3 == -2
。其它多数编程语言比如C99 采用截尾取整 规则,余数的符号同于被除数 。模除运算结果余数的定义,确使等式a == ( a // n ) * n + a % n
对于a
和n
分别为正数或负数的情况都是有效的[ 161] 。余数可以为负数,显著不同于在数学中的欧几里得除法 规则下,余数总是非负数的情况。
Python提供了round()
内建函数,用于把一个浮点数修约 成最近的整数[ 162] ,自Python 3.0以来,为了打破平局它采用了IEEE 754 的约半成偶 规则:round(1.5)
和round(2.5)
都产生2
。
Python允许由比较运算链接起来的布尔表达式,表现得如在数学中常用的一样。比如表达式a < b < c
,测试a < b and b < c
[ 122] 。C语言将它解析为(a < b) < c
:即首先求值a < b
,其结果为0
或1
,接着把这个结果比较于c
[ 163] 。
Python对所有整数运算,使用任意精度算术 。在decimal
模块中的Decimal
类[ 164] ,提供十进制浮点数 ,具有用户可按需要而更改的缺省28个十进制 有效 数位 精度,并有多种修约方式[ 165] 。在fractions
模块中的Fraction
类,提供任意精度的有理数 [ 166] 。第三方库gmpy2[ 167] ,提供了到任意精度计算 库GMP /MPIR 、MPFR 和MPC的接口。
除了求绝对值函数abs()
列入内建函数之外,大多数数学函数,处于math
和cmath
模块内。前者用于实数运算,而后者用于复数运算。[ ak] 由于Python有着广泛的数学库,特别是第三方库NumPy 进一步扩展了固有能力,Python经常被用作科学脚本语言,来处理如数值数据处理和操纵等问题[ 168] [ 169] 。
标准库
Python拥有一个强大的标准库[ 170] 。Python标准库包括了如下功能:
程序代码实例
一個在標準輸出設備上輸出Hello World 的簡單程式,這種程式通常作為開始學習程式語言時的第一個程式,可将如下代码录入纯文本文件并随意命名比如program01.py
,然后执行这个程序python3 program01.py
:
Python也可以單步直譯執行。執行Python直譯器進入互動式命令列的環境,你可以在提示符號>>>
旁輸入print("Hello, world!")
,按Enter鍵輸出結果:
>>> print ( 'Hello, world!' )
Hello, world!
计算正数的阶乘 的程序代码:
n = int ( input ( '輸入一個數,就會印出其階乘: ' ))
if n < 0 :
raise ValueError ( '錯誤,請輸入一個非負整數' )
fact = 1
for i in range ( 2 , n + 1 ):
fact *= i
print ( fact )
注意,在Python 3.0及以上版本中,print
是个函数,需要在要打印的字符串前后加上圆括号;在Python 2.6以下版本中,print
是一个关键字和命令而不加圆括号。
实现
Python是一门跨平台的脚本语言,Python规定了一个Python语法规则,根据该规则可编写Python直譯器 [ 171] 。Python属于动态语言 ,其官方实现将Python程序编译成中间形式的字节码 [ 172] ,并接着在它的虚拟机 上执行[ 173] ,相较于C/C++和java的等编译语言 而言运行速度较慢[ 174] 。
活跃开发的实现
到其他语言的交叉编译器
其他实现举例:Jython ,它是用Java 实现的Python 2.7。IronPython ,它是建造在DLR 之上的Python 2.7和Python 3.4实现。Stackless Python ,它是实现微线程 的CPython 3.8分叉。Pyston,它是具有JIT 等性能优化的CPython 3.8.12的分叉[ 188] 。Pyjion,将Python代码编译成本机CIL 的CPython 3.10的JIT 扩展[ 189] 。Cinder,它是Meta孵化器 发布的具有包括JIT 等很多优化的CPython 3.10分叉[ 190] 。
开发环境
通用文本编辑器
很多并非集成开发环境软件的文本编辑器 ,也对Python有不同程度的支持,并且加上专门为Python设计的编辑器插件 也会有很高的可用性。
专用开发环境
适用于Python的集成开发环境 (IDE)软件,除了标准二进制发布包所附的IDLE之外,还有许多其他选择。其中有些软件设计有语法着色、语法检查、运行调试 、自动补全、智能感知等便利功能。由于Python的跨平台 出身,这些软件往往也具备各种操作系统的版本或一定的移植性。
IDLE :Python“标准”IDE,一般随Python而安装,支持较少的编辑功能,调试功能也比较弱。
Eric :基于PyQt 的自由的IDE,支持自动补全、智能感知、自动语法检查、工程管理、svn/mercurial集成、自动单元测试等功能,具有可扩展的插件系统,通过可选插件支持Git 集成。调试功能与Visual Studio和Eclipse类似。
Spyder :开源的跨平台科学计算IDE。
PyScripter :功能较全的开源IDE,使用Delphi开发。
PyCharm :由JetBrains 公司出品,具备一般IDE的功能,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版本控制等等,另外,它还提供了一些功能用于Django开发,同时支持Google App Engine ,还支持IronPython。它是商业软件,但也具有社区版和教育版。
Thonny :适用于编程初学者的IDE。
Wing IDE :商业软件,有免費的功能有限的Wing 101。
第三方扩展包
Python Powered
Python社群提供了大量的功能覆盖众多领域的第三方模組,其使用方式与标准库类似。第三方模块可以使用Python/Cython或者C 语言编写。软件工具SWIG 和SIP ,通过定义接口文件或规定文件的方式,可以将C /C++ 编写的程序库包装为Python模块。Python解释器本身也可以被集成到其它需要腳本語言的程式内。
Python包索引 是公开的软件包在线仓库。pip 是官网推荐的以安全方式安装Python应用及其依赖软件包的最流行工具[ 191] 。要安装在整个操作系统范围内共享的Python包,现在需要通过操作系统的软件包管理系统 。要将特定于应用的依赖包隔离于共享的Python安装,可以使用标准库的venv[ 192] 或第三方工具virtualenv[ 193] 创建虚拟环境;第三方工具pipenv,能自动为用户项目建立和管理虚拟环境,并在安装/卸装软件包的时候,向此项目的Pipfile文件增加/移除这个软件包[ 194] 。
網絡服务
Python定義了WSGI 標準應用接口,来協調HTTP 伺服器與基於Python的Web程式之間的溝通。比如,通過mod_wsgi 模組,Apache 可以運行用Python編寫的Web程式。Zope 是著名的用Python编写的开源的Web应用服务器 。Tornado 是用Python语言写成的非阻塞式web服务器,也是轻量级的Web框架。
Python對於各种網路協定的支援很完善,因此適用於編寫伺服器軟體、網路爬蟲等Web開發。用Python编写的一些Web框架,有助於輕鬆地開發和管理複雜的Web程式。著名的第三方Web框架 和函数库:
图形用户界面
Python本身包含了Tkinter 库,它是Python的业界标准GUI 并被集成进入了IDLE 。Tkinter基于了Tcl 命令工具,能够支持简单的GUI 开发。但是为了让所开发的软件运行速度更快,并与用户的桌面环境更契合,人们一般会选择采用第三方GUI库或框架。著名的第三方GUI库:
数据科学
重要的数据科学 用第三方软件库有:
数据可视化
主要的数据可视化 软件库及仪表板 框架有[ 210] :
机器学习
基础性的机器学习 软件库及框架有:
其它种类
应用
在很多作業系統裡,Python是標準的系统元件,它被列入Linux标准规范 之中[ 255] 。大多數Linux發行版 和macOS 都集成了Python,可以在终端模拟器 或虚拟控制台 下直接執行Python。第三方工具pipx,可以将Python应用安装于隔离的环境中并在其中运行它[ 256] 。
雖然Python可被粗略地分類為腳本語言 ,Python的支持者較喜歡稱它為一種高階動態語言 ,常像“胶水”一样被用来连接软件组件 ,已经显著的区别于Unix shell 、Windows PowerShell 这样的语言。基于Python的xonsh,是跨平台的、青睐Unix的shell 语言 和命令行界面 [ 257] 。
应用程序
一些Linux發行版,使用Python語言編寫安裝器,比如Ubuntu 的Ubiquity 和Fedora 的Anaconda ;或使用它編寫軟件包管理系统 ,比如Gentoo 的Portage 。如下著名应用使用Python编写或将它作为嵌入式脚本:
人工智能
经由Python开发了众多的人工智能 模型和作为其支撑的软件库:
社群流行
自从2003年,Python始终排行于TIOBE编程社区索引 前十最流行编程语言,在2021年10月它首次达到了第一名最流行语言(居于C 和Java 之前)[ 270] ,并被选为2007年、2010年、2018年、2020年、2021年和2024年的年度编程语言[ 270] 。它有如下著名的社群:
影響的语言
Python的设计和哲学已经影响了很多其他编程语言:
代码示例
^ 縮排示例:
def is_even ( a : int ) -> bool :
"""确定数a是否是偶数."""
if a % 2 == 0 :
print ( '偶数!' )
return True
print ( '奇数!' )
return False
# 参数比后续部份多一层缩进
def long_function_name (
var_one , var_two , var_three ,
var_four ):
# 可选的圆括号内后续行多一层缩进
if ( this_is_first_thing
and that_is_second_thing ):
do_something ()
# 可选的圆括号内后续行不额外缩进
elif ( this_is_third_thing and
that_is_fourth_thing ):
do_something_different ()
# 参数相对行首缩进一层
spam = long_function_name (
arg_one , arg_two ,
arg_three , arg_four )
# 参数按开括号界定垂直对齐
eggs = long_function_name ( arg_one , arg_two ,
arg_three , arg_four )
# 可选的闭括号位置
my_list = [
1 , 2 , 3 ,
4 , 5 , 6 ,
]
# 可选的闭括号位置
my_set = {
1 , 2 , 3 ,
4 , 5 , 6 ,
}
^ 采用了并行赋值的斐波那契数列 函数示例:
def fib ( n ): # 写出从第0项到第n项的Fibonacci系列
a , b , i = 0 , 1 , 0
while i <= n :
print ( a , end = ' ' )
a , b , i = b , a + b , i + 1
print ()
^ 产生素数的惰性生成器的示例:
from itertools import count
def generate_primes ( stop_at = 0 ):
if stop_at != 1 :
primes = [ 2 ]
yield 2
for n in count ( 3 , 2 ):
if 0 < stop_at < n :
return # 引发StopIteration例外
composite = False
for p in primes :
if not n % p :
composite = True
break
elif p ** 2 > n :
break
if not composite :
primes . append ( n )
yield n
上述函数的隐式迭代用例:
for i in generate_primes (): # 迭代于100以内所有素数上
if i > 100 :
break
print ( i )
在生成器表达式中使用上述函数,定义了一个惰性的、巨大而并非无限的搜集的示例:
from itertools import islice
primes_under_million = ( i for i in generate_primes () if i < 1000000 )
two_thousandth_prime = islice ( primes_under_million , 1999 , 2000 )
print ( next ( two_thousandth_prime ))
^ 用生成器模拟协程示例:
def produce ( n ):
try :
for item in range ( n ):
print ( f 'producing item { item } ->' )
yield item
except GeneratorExit :
return
def consume ():
item = None
try :
while True :
item = yield item
print ( f 'consuming item { item } <-' )
except GeneratorExit :
return
def main ():
r = []
t1 = produce ( 10 )
t2 = consume ()
try :
next ( t2 )
while True :
p = next ( t1 )
r += [ t2 . send ( p )]
except StopIteration :
t2 . close ()
print ( f 'result items: { r } ' )
main ()
^ 遵循EAFP(请求原谅比获得许可更容易)风格的例外处理示例:
f = None
try :
f = open ( "aFileName" , mode = "w" )
f . write ( could_make_error ()) # 不存在could_make_error()则产生错误
except IOError as error :
print ( error )
print ( "不能打开文件" )
except : # 捕获所有例外
print ( "未预期的错误" )
else : # 在没有出现例外时执行
print ( "文件写入完全成功" )
finally : # 清除行动,总是执行
if f :
f . close ()
^ 使用with
将文件作为资源来管理的示例:
from contextlib import contextmanager
@contextmanager
def opened ( filename , mode = "r" ):
try :
f = open ( filename , mode )
except IOError as error :
print ( error )
yield None
else :
try :
yield f
finally :
f . close ()
with opened ( "aFileName" , mode = "w" ) as f :
try :
f . write ( could_make_error ()) # 不存在could_make_error()则产生错误
except AttributeError :
print ( "不能打开文件" )
except : # 捕获所有例外
print ( "未预期的错误" )
else : # 在没有出现例外时执行
print ( "文件写入完全成功" )
^ 原生的协程示例:
import asyncio
import random
async def produce ( queue , n ):
for item in range ( n ):
# 生产一个项目,使用sleep模拟I/O操作
print ( f 'producing item { item } ->' )
await asyncio . sleep ( random . random ())
# 将项目放入队列
await queue . put ( item )
# 指示生产完毕
await queue . put ( None )
async def consume ( queue ):
while True :
# 等待来自生产者的项目
item = await queue . get ()
if item is None :
break
# 消费这个项目,使用sleep模拟I/O操作
print ( f 'consuming item { item } <-' )
await asyncio . sleep ( random . random ())
async def main ():
queue = asyncio . Queue ()
task1 = asyncio . create_task ( produce ( queue , 10 ))
task2 = asyncio . create_task ( consume ( queue ))
await task1
await task2
asyncio . run ( main ())
^ 局部变量示例:
>>> def spam ():
... print ( a )
...
>>> a = 10
>>> spam ()
10
>>> def spam ():
... a = 100
... print ( a )
...
>>> spam ()
100
>>> a
10
>>> def spam ():
... a *= 10
... print ( a )
...
>>> spam ()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
File "<stdin>" , line 2 , in spam
UnboundLocalError : cannot access local variable 'a' where it is not associated with a value
^ 全局变量示例:
>>> def spam ():
... global a
... a *= 10
... print ( a )
...
>>> a = 10
>>> spam ()
100
>>> a
100
>>> def spam ():
... def eggs ():
... print ( a )
... global a
... eggs ()
...
>>> a = 10
>>> spam ()
10
>>> def spam ():
... def eggs ():
... a *= 10
... print ( a )
... global a
... eggs ()
...
>>> spam ()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
File "<stdin>" , line 6 , in spam
File "<stdin>" , line 3 , in eggs
UnboundLocalError : cannot access local variable 'a' where it is not associated with a value
^ 非局部变量示例:
>>> def spam ():
... def eggs ():
... nonlocal a
... a *= 10
... print ( a )
... a = 10
... eggs ()
...
>>> spam ()
100
>>> def spam ():
... def eggs ():
... nonlocal a
... a *= 10
... print ( a )
... global a
... eggs ()
...
File "<stdin>" , line 3
SyntaxError : no binding for nonlocal 'a' found
^ 序列解包、可迭代解包和字典解包示例:
>>> a = [ 1 , 2 , 3 ]; b = [ 4 , 5 ]
>>> i , j , k = a
>>> print ( i , j , k )
1 2 3
>>> c = [ * a , b ]
>>> c
[1, 2, 3, [4, 5]]
>>> d = { * a , * b }
>>> d
{1, 2, 3, 4, 5}
>>> e = ( * a , * b )
>>> e
(1, 2, 3, 4, 5)
>>> f = { 'as' : 1 , 'bs' : 2 }; g = { 'cs' : 3 , 'ds' : 4 }
>>> h = { ** f , ** g }
>>> h
{'as': 1, 'bs': 2, 'cs': 3, 'ds': 4}
^ 列表的推导式示例。比如:
>>> [ x + 3 for x in range ( 4 )]
[3, 4, 5, 6]
快速排序 算法可以优雅的(尽管不高效的)使用列表推导式表达为:
def qsort ( L ):
if L == []:
return []
pivot = L [ 0 ]
return ( qsort ([ x for x in L [ 1 :] if x < pivot ]) +
[ pivot ] +
qsort ([ x for x in L [ 1 :] if x >= pivot ]))
^ 生成器表达式示例。比如:
>>> sum ( x * x for x in range ( 10 ))
285
>>> dict (( n , n * n ) for n in range ( 5 ))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> set ( n * n for n in range ( 5 ))
{0, 1, 4, 9, 16}
^ 字典推导式{expr1: expr2 for k, v in d}
,等价于:
result = {}
for k , v in d . items ():
result [ expr1 ] = expr2
return result
>>> { x : x + 3 for x in range ( 4 )}
{0: 3, 1: 4, 2: 5, 3: 6}
集合推导式{expr1 for x in stuff}
,等价于:
result = set ()
for x in stuff :
result . add ( expr1 )
return result
>>> { x + 3 for x in range ( 4 )}
{3, 4, 5, 6}
^ 下面几个判断语句为真,表示列表分片结果符合预期:
>>> nums = [ 1 , 3 , 5 , 7 , 8 , 13 , 20 ]
>>> nums [ 2 : 5 ] == [ 5 , 7 , 8 ] #从下标为2的元素切割到下标为5的元素,但不包含下标为5的元素。
True
>>> nums [ 1 :] == [ 3 , 5 , 7 , 8 , 13 , 20 ] #切割到最后一个元素。
True
>>> nums [: - 3 ] == [ 1 , 3 , 5 , 7 ] #从最开始的元素一直切割到倒数第3个元素。
True
>>> nums [:] == [ 1 , 3 , 5 , 7 , 8 , 13 , 20 ] #返回所有元素。改变新的列表不会影响到nums。
True
>>> nums [ 1 : 5 : 2 ] == [ 3 , 7 ] #从下标为1的元素切割到下标为5的元素,但不包含下标为5的元素,且步长为2。
True
^ 匿名函数示例:
>>> from functools import reduce
>>> reduce ( lambda x , y : x + y , [ 1 , 2 , 3 , 4 , 5 ])
15
>>> fac = lambda n : ( 1 if n < 2 else n * fac ( n - 1 ))
>>> fac ( 5 )
120
>>> [ * map ( fac , [ 1 , 2 , 3 , 4 , 5 ])]
[1, 2, 6, 24, 120]
不动点组合子 示例:
>>> Y = lambda f : ( lambda x : x ( x ))( lambda y : f ( lambda * args : y ( y )( * args )))
>>> fac = lambda f : lambda n : ( 1 if n < 2 else n * f ( n - 1 ))
>>> Y ( fac )( 5 )
120
>>> fib = lambda f : lambda n : 0 if n == 0 else ( 1 if n == 1 else f ( n - 1 ) + f ( n - 2 ))
>>> Y ( fib )( 6 )
8
>>> [ * map (( lambda f : ( lambda x : x ( x ))( lambda y : f ( lambda * args : y ( y )( * args ))))( lambda f : lambda n : ( 1 if n < 2 else n * f ( n - 1 ))), [ 1 , 2 , 3 , 4 , 5 ])]
[1, 2, 6, 24, 120]
上述Y组合子代码源出自Rosetta Code . [2020-10-21 ] . (原始内容存档 于2021-01-11). 其原理可参见Equational derivations of the Y combinator and Church encodings in Python . [2020-10-21 ] . (原始内容存档 于2020-11-12).
^ 多行字符串文字带有字符串插值 (使用了format
方法)的示例:
print ( """亲爱的 {recipient} ,
我希望你离开Sunnydale并永不返回.
不是很爱你的,
{sender}
""" . format ( sender = "吸血鬼猎人Buffy" , recipient = "Spike" ))
^ 原始字符串的示例:
>>> # Windows路径,即使是原始字符串也不能结束于反斜杠
>>> r "C:\Spam\Eggs\Ham \"
File "<stdin>" , line 1
r "C:\Spam\Eggs\Ham \"
^
SyntaxError : EOL while scanning string literal
>>> dos_path = r "C:\Spam\Eggs\Ham\ " # 通过增加尾随的空格
>>> dos_path . rstrip () # 并接着移除它来避免错误
'C:\\Spam\\Eggs\\Ham\\'
>>> quoted_dos_path = r '" {} "' . format ( dos_path )
>>> quoted_dos_path
'"C:\\Spam\\Eggs\\Ham\\ "'
>>> # 匹配具有可能的反斜杠引述的引述字符串的正则表达式
>>> import re
>>> re . match ( r '"(([^" \\ ]| \\ .)*)"' , quoted_dos_path ) . group ( 1 ) . rstrip ()
'C:\\Spam\\Eggs\\Ham\\'
>>> code = 'spam(2, eggs)'
>>> # 反转有二个参数的函数调用的两个实际参数
>>> re . sub ( r '\(([^,]*),\s*([^,]*)\)' , r '(\2, \1)' , code )
'spam(eggs, 2)'
>>> # 注意如果实际参数中有圆括号或逗号则上例无效。
^ 字符串文字串接示例:
>>> print ( "hello " 'world' )
hello world
>>> str1 = "hello " ; str2 = 'world'
>>> print ( str1 + str2 )
hello world
^ 格式化字符串的示例,例如下列命令行echo
命令:
num = "3" ; printer = "HP Laserjet"
echo "I just printed ${ num } pages to the printer ${ printer } "
等价于如下Python中的任何一种print
函数调用:
num = 3 ; printer = "HP Laserjet"
print ( f "I just printed { num } pages to the printer { printer } " )
print ( "I just printed {} pages to the printer {} " . format ( num , printer ))
print ( "I just printed {0} pages to the printer {1} " . format ( num , printer ))
print ( "I just printed {num} pages to the printer {printer} " . format ( num = num , printer = printer ))
print ( "I just printed %s pages to the printer %s " % ( num , printer ))
print ( "I just printed %(num)s pages to the printer %(printer)s " % { "num" : num , "printer" : printer })
^ 词法闭包的例子:
def f ( x ):
def g ( y ):
return x + y
return g # 返回一个闭包。
h = lambda x : lambda y : x + y
# 将指定闭包赋值给变量。
a = f ( 1 )
b = h ( 1 )
c = f ( 2 )
# 使用存储在变量中的闭包。
assert a ( 5 ) == 6
assert b ( 5 ) == 6
assert c ( 5 ) == 7
# 使用闭包而不事先把它们绑定到变量。
assert f ( 1 )( 5 ) == 6 # f(1)是个闭包。
assert h ( 1 )( 5 ) == 6 # h(1)是个闭包。
assert f ( 2 )( 5 ) == 7 # f(2)是个闭包。
^ 函数的形式参数缺省值的持久性示例:
>>> def f ( a , L = []):
... L . append ( a )
... return L
...
>>> print ( f ( 1 ))
[1]
>>> print ( f ( 2 ))
[1, 2]
>>> print ( f ( 3 ))
[1, 2, 3]
^ 函数的形实参数二者结合示例。例如:
>>> def spam ( a ):
... a . append ( 'LovelySpam' )
...
>>> def eggs ( b ):
... b = 100 #实际上是重新绑定了另一个整型对象100
...
>>> a = []
>>> b = 10
>>> spam ( a )
>>> eggs ( b )
>>> print ( a )
['LovelySpam']
>>> print ( b )
10
^ 函数的形式参数缺省值的定义示例:
>>> from inspect import getfullargspec
>>> def func ( p1 , / , p2 , p3 = "x" , * , p4 ): pass
...
>>> getfullargspec ( func )
FullArgSpec(args=['p1', 'p2', 'p3'], varargs=None, varkw=None, defaults=('x',), kwonlyargs=['p4'], kwonlydefaults=None, annotations={})
^ 函数的可变参数的定义示例:
>>> from inspect import getfullargspec
>>> def func ( p1 , / , p2 , * args , p3 , ** kwargs ): pass
...
>>> getfullargspec ( func )
FullArgSpec(args=['p1', 'p2'], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=['p3'], kwonlydefaults=None, annotations={})
^ 定义修饰器的示例:
def viking_chorus ( myfunc ):
def inner_func ( * args , ** kwargs ):
for i in range ( 3 ):
myfunc ( * args , ** kwargs )
return inner_func
调用修饰器的示例:
@viking_chorus
def menu_item ( * args ):
print ( ", " . join ( args ) + ", and spam" )
等价于:
def menu_item ( * args ):
print ( ", " . join ( args ) + ", and spam" )
menu_item = viking_chorus ( menu_item )
viking _chorus
修饰后的menu_item
将原来定义运行3次:
>>> menu_item ( "egg" , "bacon" )
egg, bacon, and spam
egg, bacon, and spam
egg, bacon, and spam
^ 修饰器工厂 示例,这里的favourite_colour
接受一个实际参数,并返回一个修饰器:
def favourite_colour ( colour ):
def decorator ( func ):
def wrapper ( * args , ** kwargs ):
print ( f "My favourite colour is { colour } ." )
func ( * args , ** kwargs )
return wrapper
return decorator
def invincible ( func ):
def wrapper ( * args , ** kwargs ):
print ( "I'm invincible!" )
func ( * args , ** kwargs )
return wrapper
使用毗连的修饰符链接修饰器示例:
@invincible
@favourite_colour ( "blue" )
def black_knight ():
print ( "None shall pass." )
使用中间变量链接修饰器示例:
blue_decorator = favourite_colour ( "blue" )
decorated_by_blue = blue_decorator ( black_knight )
black_knight = invincible ( decorated_by_blue )
它们等价于:
black_knight = invincible ( favourite_colour ( "blue" )( black_knight ))
black_knight ()结果为:
>>> black_knight ()
I'm invincible!
My favourite colour is blue.
None shall pass.
^ 调用函数使用帮助信息示例。比如:
>>> def randint ( a , b ):
... "Return random integer in range [a, b], including both end points."
...
>>> help ( randint )
Help on function randint in module __main__:
randint(a, b)
Return random integer in range [a, b], including both end points.
^ 如下这样给参数增加类型标注的提示信息:
>>> def greeting ( name : str ) -> str :
... return 'Hello ' + name
...
>>> greeting . __annotations__
{'name': <class 'str'>, 'return': <class 'str'>}
^ 对象的方法示例:
>>> class Fish ( object ):
... hungry = True
... def eat ( self , food ):
... if food is not None :
... self . hungry = False
...
>>> def status ( self ):
... print ( "Hungray!" if self . hungry else "Not hungray!" )
...
>>> e = Fish ()
>>> status ( e )
Hungray!
>>> Fish . hungry = False
>>> from types import MethodType
>>> e . status = MethodType ( status , e )
>>> e . status ()
Not hungray!
>>> Fish . hungry = True
>>> Fish . status = status
>>> f = Fish ()
>>> Fish . status ( f )
Hungray!
>>> f . eat ( "earthworm" )
>>> f . status ()
Not hungray!
^ 特殊方法和子类调用超类方法的例子:
>>> class Thought ( object ):
... cls_name = "类型Thought"
... def __init_subclass__ ( cls ):
... cls . cls_name = "类型Thought的子类型"
... def __init__ ( self , * args , ** kwargs ):
... print ( f "我是 { type ( self ) . cls_name } 的新对象!" )
... if len ( args ) != 0 or len ( kwargs ) != 0 :
... print ( f "init: nargs= { len ( args ) } , nkwargs= { len ( kwargs ) } " )
... self . notion = "我觉得我在平行宇宙中把车停歪了."
... def message ( self , * args ):
... print ( self . notion )
... if len ( args ) != 0 :
... print ( " \n " . join ( args ))
...
>>> class Advice ( Thought ):
... def message ( self ):
... super () . message ( "警告: 日历里的日期比它们看起来更近!" )
...
>>> t = Thought ()
我是类型Thought的新对象!
>>> t . message ()
我觉得我在平行宇宙中把车停歪了.
>>> a = Advice ()
我是类型Thought的子类型的新对象!
>>> a . message ()
我觉得我在平行宇宙中把车停歪了.
警告: 日历里的日期比它们看起来更近!
>>> # 内省一下:
>>> [ * super . __dict__ ]
['__new__', '__repr__', '__getattribute__', '__get__', '__init__', '__thisclass__', '__self__', '__self_class__', '__doc__']
>>> [ * super ( Advice ) . __thisclass__ . __dict__ ]
['__module__', 'message', '__doc__', 'cls_name']
>>> super ( Advice ) . __thisclass__ . __dict__ [ 'cls_name' ]
'类型Thought的子类型'
>>> [ * super ( Advice , a ) . __self__ . __dict__ ]
['notion']
>>> super ( Advice , a ) . __self_class__ . __dict__ [ 'cls_name' ]
'类型Thought的子类型'
>>> super ( Advice , a ) . message ()
我觉得我在平行宇宙中把车停歪了.
>>> super ( Advice ) . __get__ ( a ) . message ()
我觉得我在平行宇宙中把车停歪了.
^ 在类中采用数据描述器的示例:
>>> class RevealAccess :
... """正常的设置和返回值的数据描述器,
... 它还打印记录这次访问的一个消息。
... """
... def __init__ ( self , initval = None , name = 'var' ):
... self . val = initval
... self . name = name
... def __get__ ( self , obj , objtype ):
... print ( 'Retrieving' , self . name )
... return self . val
... def __set__ ( self , obj , val ):
... print ( 'Updating' , self . name )
... self . val = val
...
>>> class MyClass :
... x = RevealAccess ( 10 , 'var "x"' )
... y = 5
...
>>> m = MyClass ()
>>> m . x
Retrieving var "x"
10
>>> vars ( m )
{}
>>> MyClass . __dict__ [ 'x' ] . val
10
>>> m . x = 20
Updating var "x"
>>> vars ( m )
{}
>>> MyClass . __dict__ [ 'x' ] . val
20
>>> m . y
5
>>> vars ( m )
{}
>>> m . y = 5
>>> vars ( m )
{'y': 5}
^ 特殊方法、类方法和静态方法(__new__()
)示例:
>>> from weakref import WeakValueDictionary
>>> class D :
... _template = {}
... _obj_dict = WeakValueDictionary ()
... def __new__ ( cls , * args , ** kwargs ):
... obj = super ( D , cls ) . __new__ ( cls )
... cls . _obj_dict [ id ( obj )] = obj
... return obj
... @classmethod
... def load ( cls , dict ):
... cls . _template . update ( dict )
... @classmethod
... def create ( cls , * args , ** kwargs ):
... return cls ( cls . _template , * args , ** kwargs )
... @classmethod
... def count ( cls ):
... return len ( cls . _obj_dict )
... def __init__ ( self , dict = {}, / , * args , ** kwargs ):
... self . __dict__ . update ( dict )
... self . __dict__ . update ( kwargs )
... def __call__ ( self , * args , ** kwargs ):
... self . __dict__ . update ( kwargs )
... return self . __dict__ . copy ()
... def __len__ ( self ):
... return len ( self . __dict__ )
... def __getitem__ ( self , key ):
... return self . __dict__ [ key ]
... def __setitem__ ( self , key , value ):
... self . __dict__ [ key ] = value
...
>>> a = { 'ak' : 1 , 'bk' : 2 , 'ck' : 3 }
>>> d = D ( a , dk = 4 )
>>> d ()
{'ak': 1, 'bk': 2, 'ck': 3, 'dk': 4}
>>> D . load ( a )
>>> e = D . create ( ck = 4 )
>>> e ()
{'ak': 1, 'bk': 2, 'ck': 4}
>>> f = D ( ak = 1 , bk = 2 )
>>> f ( ck = 3 )
{'ak': 1, 'bk': 2, 'ck': 3}
>>> f [ 'ak' ]
1
>>> f [ 'ak' ] = 5
>>> f ()
{'ak': 5, 'bk': 2, 'ck': 3}
>>> len ( f )
3
>>> D . count ()
3
>>> del f
>>> D . count ()
2
>>> d . __weakref__ in D . _obj_dict . valuerefs ()
True
^ 在类中调用property()
的例子:
>>> class C :
... def __init__ ( self ):
... self . __x = None
... def getx ( self ):
... return self . __x
... def setx ( self , value ):
... self . __x = value
... def delx ( self ):
... del self . __x
... x = property ( getx , setx , delx , "I'm the 'x' property." )
...
>>> c = C ()
>>> vars ( c )
{'_C__x': None}
>>> { * C . __dict__ }
{'__init__', 'setx', '__weakref__', 'delx', 'x', 'getx', '__doc__', '__module__', '__dict__'}
上述代码可以采用修饰符进一步的书写为:
>>> class C :
... def __init__ ( self ):
... self . __x = None
... @property
... def x ( self ):
... """I'm the 'x' property."""
... return self . __x
... @x . setter
... def x ( self , value ):
... self . __x = value
... @x . deleter
... def x ( self ):
... del self . __x
...
>>> c = C ()
>>> vars ( c )
{'_C__x': None}
>>> { * C . __dict__ }
{'__init__', '__weakref__', 'x', '__doc__', '__module__', '__dict__'}
^
建立列表的特殊语法示例:
a_list = [ 1 , 2 , 3 , 'a dog' ]
采用正常的对象创建方式的示例:
a_second_list = list ()
a_second_list . append ( 4 )
a_second_list . append ( 5 )
建立元组的特殊语法示例:
a_tuple = 1 , 2 , 3 , 'four'
建立集合的特殊语法示例:
some_set = { 0 , (), False }
建立字典的特殊语法示例:
a_dictionary = { 'key 1' : 'value 1' , 2 : 3 , 4 : []}
^
两个类及元类等的实例关系(蓝色连接)与继承关系(绿色连接)示意图:
r = object
c = type
class M ( c ): pass
class A ( metaclass = M ): pass
class B ( A ): pass
b = B ()
>>> type ( b )
<class '__main__.B'>
>>> print ( type ( B ), B . __bases__ )
<class '__main__.M'> (<class '__main__.A'>,)
>>> print ( type ( A ), A . __bases__ )
<class '__main__.M'> (<class 'object'>,)
>>> print ( type ( M ), M . __bases__ )
<class 'type'> (<class 'type'>,)
>>> print ( type ( c ), c . __bases__ )
<class 'type'> (<class 'object'>,)
>>> print ( type ( r ), r . __bases__ )
<class 'type'> ()
^ 数学运算示例。比如:
>>> def mean ( seq ):
... return sum ( seq ) / len ( seq )
...
>>> mean ([ 3 , 4 ])
3.5
>>> import math
>>> print ( math . sin ( math . pi / 2 ))
1.0
註釋
^ 1.0 1.1 GitHub repository of Python 0.9.1 source . [2023-02-25 ] . (原始内容存档 于2023-03-05). HISTORY . Python source distribution. Python Foundation. [2017-11-23 ] . (原始内容存档 于2017-12-01).
^ "Python Insider: Python 3.14.0a7, 3.13.3, 3.12.10, 3.11.12, 3.10.17 and 3.9.22 are now available" ; 作者姓名字符串: Hugo; 出版日期: 2025年4月8日; 检索日期: 2025年4月9日.
^ Python 3.14.0 beta 1 is here! . 2025年5月7日 [2025年5月8日] (英語) .
^ "Python 3.14 Reaches Beta With New Tail-Call Interpreter For Better Performance" ; 检索日期: 2025年5月8日; 作品或名稱使用語言: 英語; 出版日期: 2025年5月7日; 宣传物: Phoronix.
^ 5.0 5.1 Glossary — duck-typing . [2023-06-28 ] . (原始内容存档 于2020-06-25).
^
Why is Python a dynamic language and also a strongly typed language - Python Wiki . wiki.python.org. [2021-01-27 ] . (原始内容 存档于2021-03-14).
^ 7.0 7.1 PEP 483 – The Theory of Type Hints . [2023-03-16 ] . (原始内容存档 于2023-03-26).
^ File extension .pyo was removed in Python 3.5. See PEP 0488 (页面存档备份 ,存于互联网档案馆 )
^ Holth, Moore. PEP 0441 -- Improving Python ZIP Application Support . 2014-03-30 [2015-11-12 ] . (原始内容存档 于2018-12-26).
^ 10.0 10.1 10.2 Why was Python created in the first place? . Python FAQ. [2007-03-22 ] . (原始内容存档 于2008-02-23).
^ Andrew Kuchling, Guido van Rossum. LJ Interviews Guido van Rossum . November 1, 1998 [2023-03-21 ] . (原始内容存档 于2023-03-30). String slicing came from Algol-68 and Icon.
^ 12.0 12.1
The Python Standard Library - Functional Programming Modules - itertools — Functions creating iterators for efficient looping . [2020-04-22 ] . (原始内容 存档于2020-06-14).
^
van Rossum, Guido. An Introduction to Python for UNIX/C Programmers . Proceedings of the NLUUG Najaarsconferentie (Dutch UNIX Users Group). 1993 [2021-03-14 ] . (原始内容 存档于2013-06-17). even though the design of C is far from ideal, its influence on Python is considerable.
^ 14.0 14.1
The Python Tutorial - 9. Classes . [20 February 2012] . (原始内容 存档于2012-10-23). Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics. It is a mixture of the class mechanisms found in C++ and Modula-3.
^ Fredrik Lundh. Python-list mailing list - passing by refference . [2023-03-06 ] . (原始内容存档 于2023-03-06). replace "CLU" with "Python", "record" with "instance", and "procedure" with "function or method", and you get a pretty accurate description of Python's object model.
^ 16.0 16.1
Simionato, Michele. The Python 2.3 Method Resolution Order . Python Software Foundation. [2021-03-14 ] . (原始内容 存档于2020-08-20). The C3 method itself has nothing to do with Python, since it was invented by people working on Dylan and it is described in a paper intended for lispers.
^
Kuchling, A. M. Functional Programming HOWTO . Python v2.7.2 documentation. Python Software Foundation. [9 February 2012] . (原始内容 存档于2012-10-24).
^ 18.0 18.1 18.2 Guido van Rossum. Python Reference Manual - Version 1.2 (PDF) . CWI Report CS-R9525. May 1995 [2023-03-04 ] . (原始内容存档 (PDF) 于2023-03-05). Python is a simple, yet powerful, interpreted programming language that bridges the gap between C and shell programming, and is thus ideally suited for "throw-away programming" and rapid prototyping. Its syntax is put together from constructs borrowed from a variety of other languages; most prominent are influences from ABC , C , Modula-3 and Icon . The Python interpreter is easily extended with new functions and data types implemented in C. Python is also suitable as an extension language for highly customizable C applications such as editors or window managers.
^
Smith, Kevin D.; Jewett, Jim J.; Montanaro, Skip; Baxter, Anthony. PEP 318 – Decorators for Functions and Methods . Python Enhancement Proposals. Python Software Foundation. 2 September 2004 [24 February 2012] . (原始内容 存档于2020-06-03).
^
More Control Flow Tools . Python 3 documentation. Python Software Foundation. [24 July 2015] . (原始内容 存档于2016-06-04).
^ re — Regular expression operations . docs.python.org. [2022-09-06 ] . (原始内容存档 于2018-07-18). This module provides regular expression matching operations similar to those found in Perl.
^
CoffeeScript . coffeescript.org. [2021-03-14 ] . (原始内容 存档于2020-06-12).
^ 23.0 23.1 FAQ: What is GDScript and why should I use it? . [2020-09-13 ] . (原始内容存档 于2020-05-19).
^
The Genie Programming Language Tutorial . [28 February 2020] . (原始内容 存档于2020-06-01).
^ 25.0 25.1 Frequently Asked Questions (FAQ) - The Go Programming Language . The Go Programming Language. [February 26, 2016] . (原始内容存档 于2020-05-03).
^
Perl and Python influences in JavaScript . www.2ality.com. 24 February 2013 [15 May 2015] . (原始内容 存档于2018-12-26).
^ Rauschmayer, Axel. Chapter 3: The Nature of JavaScript; Influences . O'Reilly, Speaking JavaScript. [15 May 2015] . (原始内容 存档于2018-12-26).
^ 28.0 28.1 Home · The Julia Language . docs.julialang.org. [2018-08-15 ] . (原始内容存档 于11 January 2021) (英语) .
^ Krill, Paul. Mojo language marries Python and MLIR for AI development . InfoWorld. 2023-05-04 [2023-05-05 ] . (原始内容存档 于5 May 2023) (英语) .
^ 30.0 30.1 Bini, Ola. Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java . Berkeley: APress. 2007: 3 . ISBN 978-1-59059-881-8 . It draws primarily on features from Perl, Smalltalk, Python, Lisp, Dylan, and CLU.
^ 31.0 31.1 Starlark Language . [25 May 2019] . (原始内容 存档于2020-06-15).
^ 32.0 32.1
Lattner, Chris. Chris Lattner's Homepage . Chris Lattner. 3 June 2014 [3 June 2014] . (原始内容 存档于2018-12-25). Of course, it also greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.
^ 彼德·諾米格 . Python for Lisp Programmers . [2020-04-21 ] . (原始内容存档 于2020-06-14). Python can be seen as a dialect of Lisp with "traditional" syntax (what Lisp people call "infix" or "m-lisp" syntax).
^ Rossum, Guido Van. The History of Python: A Brief Timeline of Python . The History of Python. 2009-01-20 [2021-03-05 ] . (原始内容存档 于5 June 2020).
^ Peterson, Benjamin. Python Insider: Python 2.7.18, the last release of Python 2 . Python Insider. 20 April 2020 [27 April 2020] . (原始内容存档 于26 April 2020).
^ Stack Overflow Developer Survey 2020 . Stack Overflow. [2021-03-05 ] . (原始内容存档 于2 March 2021).
^ The State of Developer Ecosystem in 2020 Infographic . JetBrains: Developer Tools for Professionals and Teams. [2021-03-05 ] . (原始内容存档 于1 March 2021) (英语) .
^ index | TIOBE - The Software Quality Company . www.tiobe.com. [2021-02-02 ] . (原始内容存档 于25 February 2018). Python has won the TIOBE programming language of the year award! This is for the fourth time in the history, which is a record! The title is awarded to the programming language that has gained most popularity in one year.
^ PYPL PopularitY of Programming Language index . pypl.github.io. [2021-03-26 ] . (原始内容存档 于14 March 2017) (英语) .
^ Guido van Rossum - Resume . [2022-10-12 ] . (原始内容存档 于2022-12-21).
^ amoeba operating system . [2023-02-25 ] . (原始内容存档 于2023-03-05).
^ Whetting Your Appetite . The Python Tutorial. Python Software Foundation. [2012-02-20 ] . (原始内容存档 于2012-10-26).
^ Benevolent dictator for life . Linux Format . 2005-02-01 [2007-11-01 ] . (原始内容 存档于2006-10-01).
^ Transfer of power . [2020-11-29 ] . (原始内容存档 于2018-07-12).
^ PEP 8100 . python. Python Software Foundation. [2019-05-04 ] . (原始内容存档 于2020-06-04).
^ Steering Council nomination: Guido van Rossum (2020 term) . [2020-10-11 ] . (原始内容存档 于2019-12-29).
^ The Making of Python . Artima Developer. [2007-03-22 ] . (原始内容存档 于2016-09-01).
^ Older source releases (1.0.1 - 1.6) . [2023-03-05 ] . (原始内容存档 于2023-03-05).
^ HISTORY . [2020-09-12 ] . (原始内容存档 于2017-12-01). Lambda expressions are particularly useful in combination with map(), filter() and reduce(), described below. Thanks to Amrit Prem for submitting this code (as well as map(), filter(), reduce() and xrange())!
^ LJ #37: Python 1.4 Update . [2007-04-29 ] . (原始内容 存档于2007-05-01).
^ Kuchling, A. M.; Zadka, Moshe. What's New in Python 2.0 . Python Software Foundation. 2000-10-16 [2012-02-11 ] . (原始内容存档 于2012-10-23).
^ Hylton, Jeremy. PEP 227 - Statically Nested Scopes . 2000-11-01 [2007-03-22 ] . (原始内容存档 于2007-03-29).
^ 53.0 53.1 53.2 Guido van Rossum. Unifying types and classes in Python 2.2 . [2020-09-26 ] . (原始内容存档 于2019-09-28).
^ What’s New in Python 2.2 - PEP 234: Iterators . [2020-10-21 ] . (原始内容存档 于2021-02-07).
^ What’s New in Python 2.2 - PEP 255: Simple Generators . [2020-10-21 ] . (原始内容 存档于2021-02-07).
^ What’s New in Python 2.2 - Descriptors . [2020-10-21 ] . (原始内容存档 于2021-02-07).
^ 57.0 57.1 57.2 What’s New in Python 2.4 - PEP 318: Decorators for Functions and Methods . [2020-10-21 ] . (原始内容存档 于2021-02-07).
^ What’s New in Python 2.5 - PEP 343: The ‘with’ statement . [2020-10-21 ] . (原始内容存档 于2021-02-07).
^ PEP 339 – Design of the CPython Compiler . 2005 [2023-03-06 ] . (原始内容存档 于2023-03-06).
^ Guido van Rossum. What’s New In Python 3.0 . [2023-02-18 ] . (原始内容存档 于2012-11-02).
^ What’s New In Python 3.4 - asyncio . [2023-02-22 ] . (原始内容存档 于2023-06-13).
^ What’s New In Python 3.5 - PEP 484 - Type Hints . [2023-02-25 ] . (原始内容存档 于2016-06-18).
^ PEP 492 - Coroutines with async and await syntax . [2019-11-21 ] . (原始内容存档 于2019-01-05).
^ 64.0 64.1 What’s New in Python 3.8 - Assignment expressions . [2023-03-08 ] . (原始内容存档 于2021-06-01).
^ Paul Krill, Guido Van Rossum. Guido van Rossum resigns: What’s next for Python . Jul 27, 2018 [2023-03-19 ] . (原始内容存档 于2023-03-19).
^ PEP 585 – Type Hinting Generics In Standard Collections . [2023-03-17 ] . (原始内容存档 于2023-05-25). The Python Standard Library - Built-in Types - Generic Alias Type . [2023-03-17 ] . (原始内容存档 于2020-06-14).
^ What’s New In Python 3.9 - New Parser . [2023-02-22 ] . (原始内容存档 于2020-10-07).
^ What’s New In Python 3.10 - PEP 634: Structural Pattern Matching . [2023-02-22 ] . (原始内容存档 于2023-06-11).
^ PEP 604 – Allow writing union types as X | Y . [2023-03-17 ] . (原始内容存档 于2023-05-26). The Python Standard Library - Built-in Types - Union Type . [2023-03-17 ] . (原始内容存档 于2020-06-14).
^ What’s New In Python 3.11 - Faster CPython . [2023-03-24 ] . (原始内容存档 于2023-06-13).
^ What’s New In Python 3.12 — PEP 695: Type Parameter Syntax . [2023-10-20 ] . (原始内容存档 于2023-11-13).
^ What’s New In Python 3.13 . [2024-09-17 ] . (原始内容存档 于2024-09-29).
^ Python Developer's Guide — Status of Python Version . devguide.python.org. [2022-08-26 ] . (原始内容存档 于2022-08-26).
^ PEP 8106 – 2025 Term Steering Council election . Python Software Foundation. [2024-12-11 ] . (原始内容存档 于2025-01-30).
^ 75.0 75.1 The Python Language Reference - 3. Data model - Special method names . [2020-09-25 ] . (原始内容存档 于2012-10-26).
^ aspectlib . [2020-09-28 ] . (原始内容 存档于2014-11-05). aspectlib is an aspect-oriented programming, monkey-patch and decorators library. It is useful when changing behavior in existing code is desired.
^ PEP 316 -- Programming by Contract for Python . [2021-01-17 ] . (原始内容存档 于2021-01-22).
Deal . [2021-06-20 ] . (原始内容 存档于2021-07-16). python library for design by contract (DbC) and checking values, exceptions, and side-effects.
icontract . [2021-01-17 ] . (原始内容存档 于2021-01-21). icontract provides design-by-contract to Python3 with informative violation messages and inheritance.
PyContracts . [2021-01-17 ] . (原始内容存档 于2021-01-22). PyContracts is a Python package that allows to declare constraints on function parameters and return values.
dpcontracts . [2021-01-17 ] . (原始内容存档 于2021-01-22). This module provides a collection of decorators that makes it easy to write software using contracts.
^ PyDatalog . [2012-07-22 ] . (原始内容存档 于2020-06-13).
^ Extending Python with C or C++ — Reference Counting in Python . Docs.python.org. [2020-06-05 ] . (原始内容存档 于2012-10-18) (英语) .
^ CPython’s internals — Garbage collector design — Optimization: generations . [2024-09-17 ] . (原始内容存档 于2025-01-03).
^ Functional Programming HOWTO . [2020-09-26 ] . (原始内容存档 于2012-10-24).
^ van Rossum, Guido. The fate of reduce() in Python 3000 . Artima Developer. [2007-03-22 ] . (原始内容存档 于2007-04-07).
^ Functional Programming Modules . Docs.python.org. [2020-09-26 ] . (原始内容存档 于2020-09-20).
^ Guido van Rossum. Foreword for "Programming Python" (1st ed.) . 1996-05-01 [2020-09-12 ] . (原始内容存档 于2014-07-24). Despite all its elegance and power and the availability of a free implementation, ABC never became popular in the Unix/C world. I can only speculate about the reasons, but here's a likely one: the difficulty of adding new "primitive" operations to ABC.
^ Guido van Rossum. Extending and Embedding the Python Interpreter - Version 1.2 (PDF) . CWI Report CS-R9527. May 1995 [2023-03-05 ] . (原始内容存档 (PDF) 于2023-03-05). Python is an interpreted object oriented programming language. This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can define new functions but also new object types and their methods. The document also describes how to embed the Python interpreter in another application, for use as an extension language. Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.
^ The Python Standard Library - Generic Operating System Services - ctypes — A foreign function library for Python . [2022-01-14 ] . (原始内容 存档于2022-04-27).
^ PEP 523 – Adding a frame evaluation API to CPython . [2022-08-29 ] . (原始内容存档 于2022-08-29).
^ 88.0 88.1 The Python Language Reference - 2. Lexical analysis - Explicit line joining . [2023-02-17 ] . (原始内容存档 于2018-01-09).
^ The Python Language Reference - 2. Lexical analysis - Comments . [2023-03-11 ] . (原始内容存档 于2018-01-09).
^ The Python Language Reference - 7. Simple statements . [2020-10-30 ] . (原始内容存档 于2023-06-06). The Python Language Reference - 10. Full Grammar specification . [2023-02-17 ] . (原始内容存档 于2023-06-06). simple_stmts:
| simple_stmt !';' NEWLINE # Not needed, there for speedup
| ';'.simple_stmt+ [';'] NEWLINE
^ The Python Language Reference - 8. Compound statements . [2023-02-18 ] . (原始内容存档 于2019-11-27).
^ The Python Language Reference - 10. Full Grammar specification . [2023-02-17 ] . (原始内容存档 于2023-06-06). statements: statement+
statement: compound_stmt | simple_stmts
……block:
| NEWLINE INDENT statements DEDENT
| simple_stmts
^ PEP 8 – Style Guide for Python Code - Indentation . [2020-09-26 ] . (原始内容存档 于2018-07-13).
^ The Python Language Reference - 2. Lexical analysis - Indentation . [2023-02-18 ] . (原始内容存档 于2018-01-09).
^ The Python Language Reference - 2. Lexical analysis - Keywords . [2020-09-26 ] . (原始内容存档 于2018-01-09).
^ What’s New In Python 3.0 - Overview Of Syntax Changes . [2023-02-18 ] . (原始内容存档 于2012-11-02).
^ What’s What’s New In Python 3.5 - New Keywords . [2016-06-01 ] . (原始内容存档 于2016-06-18).
^ What’s New In Python 3.7 . [2019-11-21 ] . (原始内容存档 于2019-11-28).
^ The Python Language Reference - 2. Lexical analysis - Soft Keywords . [2023-03-03 ] . (原始内容存档 于2018-01-09).
^ PEP 8 – Style Guide for Python Code - Naming Conventions . [2017-10-06 ] . (原始内容存档 于2018-07-13).
^ The Python Language Reference - 2. Lexical analysis - Reserved classes of identifiers . [2023-03-03 ] . (原始内容存档 于2018-01-09).
^ 102.0 102.1 9.6. Private Variables . [2020-09-27 ] . (原始内容存档 于2012-10-23).
^ The Python Language Reference - 7. Simple statements - Augmented assignment statements . [2023-03-08 ] . (原始内容存档 于2023-06-06).
^ The Python Tutorial - 5. Data Structures - Tuples and Sequences . [2018-10-17 ] . (原始内容存档 于2020-06-10).
^ The Python Language Reference - 6. Expressions - Yield expressions . [2023-03-15 ] . (原始内容存档 于2022-09-07).
^ 106.0 106.1 PEP 342 - Coroutines via Enhanced Generators . [2019-11-21 ] . (原始内容存档 于2020-05-29).
^ The Python Language Reference - 7. Simple statements - The yield statement . [2023-03-15 ] . (原始内容存档 于2023-06-06).
^ PEP 380 - Syntax for Delegating to a Subgenerator . [2019-11-21 ] . (原始内容存档 于2020-06-04).
^ PEP 3156 – Asynchronous IO Support Rebooted: the “asyncio” Module - Coroutines . [2019-11-21 ] . (原始内容存档 于2019-11-14).
^ Generator-based Coroutines . [2020-10-29 ] . (原始内容存档 于2018-12-31). Support for generator-based coroutines is deprecated and is scheduled for removal in Python 3.10.
^ PEP 525 – Asynchronous Generators . [2023-03-16 ] . (原始内容存档 于2023-05-21).
^ 112.0 112.1 The Python Language Reference - 7. Simple statements - The import Statement . [2020-10-30 ] . (原始内容存档 于2021-02-07).
^ 113.0 113.1 113.2 The Python Tutorial - 6. Modules . [2020-10-30 ] . (原始内容存档 于2021-02-06).
^ What’s New In Python 3.11 - PEP 654: Exception Groups and except* . [2023-10-20 ] . (原始内容存档 于2023-06-13).
^ PEP 343 – The “with” Statement . [2020-09-26 ] . (原始内容存档 于2014-12-14).
^ The Python Language Reference - 5. The import system - Packages . [2023-03-07 ] . (原始内容存档 于2022-08-27).
^ The Python Standard Library - Python Runtime Services - __main__ — Top-level code environment . [2020-10-30 ] . (原始内容存档 于2021-02-07).
^ 118.0 118.1 118.2 118.3 The Python Language Reference - 4. Execution model - Naming and binding . [2023-03-06 ] . (原始内容存档 于2022-09-08).
^ The Python Standard Library - Built-in Functions . [2020-09-25 ] . (原始内容存档 于2012-10-25).
^ 120.0 120.1 What’s New In Python 3.5 - PEP 465 - A dedicated infix operator for matrix multiplication . [2023-03-08 ] . (原始内容存档 于2016-06-18).
^ NumPy Reference - Routines - Linear algebra (numpy.linalg) - numpy.matmul . [2022-01-18 ] . (原始内容 存档于2022-04-10).
^ 122.0 122.1 The Python Language Reference - 6. Expressions - Comparison . [2023-03-21 ] . (原始内容存档 于2022-09-07).
^ The Python Language Reference - 6. Expressions - Displays for lists, sets and dictionaries . [2023-03-13 ] . (原始内容存档 于2022-09-07).
^ What’s New In Python 3.5 - PEP 448 - Additional Unpacking Generalizations . [2023-03-08 ] . (原始内容存档 于2016-06-18).
^ Hettinger, Raymond. PEP 289 – Generator Expressions . Python Enhancement Proposals. Python Software Foundation. 2002-01-30 [2012-02-19 ] . (原始内容存档 于2020-06-14).
^ The Python Language Reference - 6. Expressions - Parenthesized forms . [2023-03-13 ] . (原始内容存档 于2022-09-07).
^ The Python Language Reference - 6. Expressions - Assignment expressions . [2023-03-08 ] . (原始内容存档 于2022-09-07).
^ The Python Language Reference - 6. Expressions - Operator precedence . [2023-03-12 ] . (原始内容存档 于2022-09-07).
^ The Python Standard Library - Built-in Types - Common Sequence Operations . [2023-03-17 ] . (原始内容存档 于2020-06-14).
^ What’s New in Python 3.9 - Dictionary Merge & Update Operators . [2021-06-12 ] . (原始内容存档 于2020-10-07).
^ The Python Language Reference - 2. Lexical analysis - String literal concatenation . [2023-03-12 ] . (原始内容存档 于2018-01-09).
^ The Python Standard Library - Built-in Types - printf-style String Formatting . [2023-03-21 ] . (原始内容存档 于2020-06-14).
^ What’s New In Python 3.0 - PEP 3101: Advanced String Formatting . [2023-03-19 ] . (原始内容存档 于2012-11-02). The Python Standard Library - Text Processing Services - string — Common string operations - Format String Syntax . [2023-03-21 ] . (原始内容存档 于2023-06-06).
^ The Python Language Reference - 2. Lexical analysis - Formatted string literals . [2023-03-13 ] . (原始内容存档 于2018-01-09).
^ PEP 498 - Literal String Interpolation . python.org. [2017-03-08 ] . (原始内容存档 于2020-06-15).
^ van Rossum, Guido. Tail Recursion Elimination . Neopythonic.blogspot.be. 2009-04-22 [2012-12-03 ] . (原始内容存档 于2018-05-19). van Rossum, Guido. Language Design Is Not Just Solving Puzzles . Artima forums. Artima. 2006-02-09 [2007-03-21 ] . (原始内容存档 于2020-01-17).
^ returns documentation — Trampolines . [2024-09-28 ] . (原始内容存档 于2024-12-23).
^ The Python Tutorial - 4. More Control Flow Tools - Defining Functions . [2023-03-06 ] . (原始内容存档 于2023-06-10).
^ Positional-only parameters . [2020-09-24 ] . (原始内容 存档于2020-06-08).
^ Python 2.4 Decorators: Reducing code duplication and consolidating knowledge . Dr. Dobb's. 2005-05-01 [2007-02-08 ] . (原始内容存档 于2007-02-06).
^ The Python Standard Library - Functional Programming Modules - functools — Higher-order functions and operations on callable objects . [2023-03-18 ] . (原始内容存档 于2021-06-02).
^ PEP 3107 -- Function Annotations . [2021-03-01 ] . (原始内容 存档于2015-01-06).
^ 143.0 143.1 PEP 484 -- Type Hints . [2021-03-01 ] . (原始内容 存档于2021-02-09).
^ PEP 3115 – Metaclasses in Python 3000 . [2023-03-21 ] . (原始内容存档 于2023-04-02).
^ The Python Language Reference - 3. Data model - Customizing class creation . [2023-03-21 ] . (原始内容存档 于2012-10-26). PEP 487 – Simpler customisation of class creation . [2023-03-21 ] . (原始内容存档 于2023-03-21).
^ The Python Standard Library - Built-in Types - Special Attributes . [2020-09-12 ] . (原始内容存档 于2020-06-14).
^ Why must 'self' be used explicitly in method definitions and calls? . Design and History FAQ. Python Software Foundation. [2012-02-19 ] . (原始内容存档 于2012-10-24).
^ The Python Standard Library - Built-in Functions - class super . [2020-09-25 ] . (原始内容存档 于2018-10-26).
^ Descriptor HowTo Guide . [2020-09-26 ] . (原始内容存档 于2020-09-18).
^ The Python Standard Library - Built-in Functions - class property . [2020-09-25 ] . (原始内容存档 于2018-10-26).
^ The Python Language Reference - 3. Data model - The standard type hierarchy .
^ The Python Language Reference, section 3.3. New-style and classic classes, for release 2.7.18 . [2020-09-25 ] . (原始内容存档 于2020-05-05).
^ 153.0 153.1 mypy - Optional Static Typing for Python . [2017-01-28 ] . (原始内容存档 于2020-06-06).
^ Moshe Zadka, Guido van Rossum. PEP 237 - Unifying Long Integers and Integers . 2001-03-11. (原始内容存档 于2020-05-28).
^ The Python Tutorial - 15. Floating Point Arithmetic: Issues and Limitations . [2023-03-23 ] . (原始内容存档 于2023-06-02). almost all platforms map Python floats to IEEE-754 “double precision”.
^ The Python Standard Library - Built-in Types - Ranges . [2019-10-03 ] . (原始内容存档 于2020-06-14).
^ NumPy fundamentals - Indexing on ndarrays - Dimensional indexing tools . [2023-03-23 ] . (原始内容存档 于2023-06-01).
^ The Python Standard Library - Built-in Types - The Null Object . [2023-03-24 ] . (原始内容存档 于2023-03-24).
^ Moshe Zadka, Guido van Rossum. PEP 238 - Changing the Division Operator . 2001-03-11 [2013-10-23 ] . (原始内容存档 于2020-05-28).
^ Guido van Rossum. Why Python's Integer Division Floors . 24 August 2010 [25 August 2010] . (原始内容存档 于5 June 2020).
^ Guido van Rossum. Python Reference Manual - Version 1.2 (PDF) . CWI Report CS-R9525. May 1995 [2023-03-04 ] . (原始内容存档 (PDF) 于2023-03-05). The modulo operator always yields a result with the same sign as its second operand (or zero); …… The integer division and modulo operators are connected by the following identity: x == (x/y)*y + (x%y)
.
^ The Python Standard Library - Built-in Functions - round() , [2023-03-21 ] , (原始内容存档 于2012-10-25)
^ Brian Kernighan , Dennis Ritchie . The C Programming Language, Second Edition (PDF) . Prentice Hall. 1988 [2023-03-23 ] . (原始内容存档 (PDF) 于2023-03-25). The relational operators group left-to-right, but this fact is not useful; a<b<c
is parsed as (a<b)<c
, and evaluates to either 0
or 1
.
^ PEP 327 – Decimal Data Type . [2015-09-26 ] . (原始内容存档 于2020-06-04).
^ The Python Standard Library - Numeric and Mathematical Modules - decimal — Decimal fixed point and floating point arithmetic . [2023-03-21 ] . (原始内容存档 于2023-06-13).
^ The Python Standard Library - Numeric and Mathematical Modules - fractions — Rational numbers . [2023-03-21 ] . (原始内容存档 于2023-05-26).
^ gmpy2 is an optimized, C-coded Python extension module that supports fast multiple-precision arithmetic . [2024-10-14 ] . (原始内容存档 于2024-12-19).
^ 10 Reasons Python Rocks for Research (And a Few Reasons it Doesn't) – Hoyt Koepke . www.stat.washington.edu. [2019-02-03 ] . (原始内容存档 于2020-05-31).
^ Shell, Scott. An introduction to Python for scientific computing (PDF) . 2014-06-17 [2019-02-03 ] . (原始内容存档 (PDF) 于2019-02-04).
^ The Python Standard Library . [2023-03-09 ] . (原始内容存档 于2023-06-06).
^ Python Interpreters Benchmarks . [2022-08-30 ] . (原始内容存档 于2022-08-20).
^ Python Bytecode Instructions . Docs.python.org. [2016-02-16 ] . (原始内容存档 于2020-06-05). Python module to generate and modify bytecode . [2023-02-22 ] . (原始内容存档 于2023-04-04).
^ Obi Ike-Nwosu. Inside The Python Virtual Machine . [2023-01-29 ] . (原始内容存档 于2021-01-29). version 2019-03-02 (PDF) . [2023-01-30 ] . (原始内容存档 (PDF) 于2023-03-05). 中文翻译 . [2023-01-30 ] . (原始内容存档 于2023-03-05).
^ The Computer Language Benchmarks Game . [2023-01-31 ] . (原始内容存档 于2023-05-22).
^ Glossary — global interpreter lock . [2023-09-19 ] . (原始内容存档 于2020-06-25).
^ Codon - A high-performance, zero-overhead, extensible Python compiler using LLVM . [2023-03-24 ] . (原始内容存档 于2023-05-25).
^ Differences with Python . [2024-09-26 ] . (原始内容存档 于2023-05-25).
^ Rachel Gordon. Python-based compiler achieves orders-of-magnitude speedups . MIT CSAIL. March 14, 2023 [2023-03-25 ] . (原始内容存档 于2023-03-26).
^ Pyodide is a Python distribution for the browser and Node.js based on WebAssembly . [2023-12-28 ] . (原始内容存档 于2024-04-03).
^
RustPython . [2022-03-04 ] . (原始内容 存档于2022-04-24).
^ Brython (Browser Python) is an implementation of Python 3 running in the browser . [2023-06-17 ] . (原始内容存档 于2023-10-28).
^ Cython Users Guide — Using Parallelism . [2023-09-19 ] . (原始内容存档 于2023-10-11).
^ mypyc — Compile type annotated Python to fast C extensions . [2024-09-26 ] . (原始内容存档 于2025-01-20).
^ Pythran - Ahead of Time compiler for numeric kernels . [2022-08-29 ] . (原始内容存档 于2022-08-29).
^ Nuitka is a Python compiler written in Python . [2023-09-18 ] . (原始内容存档 于2023-11-11).
^ Transcrypt — Python 3.9 to JavaScript compiler - Lean, fast, open! . [2023-09-18 ] . (原始内容存档 于2023-09-19).
^ MyHDL - From Python to Silicon! . [2023-03-05 ] . (原始内容存档 于2023-06-05).
^ Pyston - A faster and highly-compatible implementation of the Python programming language . [2022-08-29 ] . (原始内容存档 于2022-08-29). Pyston was started at Dropbox in 2014 ……. …… In 2017 …… Pyston project was shut down. …… In 2019 the Pyston developers regrouped ……. …… In mid-2021 the Pyston developers joined Anaconda …….
^ Pyjion - A JIT for Python based upon CoreCLR . [2022-08-29 ] . (原始内容存档 于2022-08-29).
^ Cinder is Meta's internal performance-oriented production version of CPython . [2022-08-26 ] . (原始内容存档 于2021-05-04).
^ Tool recommendations . python.org. [21 April 2022] . (原始内容存档 于2022-09-06) (英语) .
^ venv — Creation of virtual environments . [2023-05-07 ] . (原始内容存档 于2023-06-13).
^ virtualenv — A tool for creating isolated virtual python environments . [2023-05-07 ] . (原始内容存档 于2023-06-11).
^ Pipenv — Python Development Workflow for Humans . [2022-08-29 ] . (原始内容存档 于2019-11-17).
^ FAQ: Django appears to be a MVC framework, but you call the Controller the “view”, and the View the “template”. How come you don’t use the standard names? . [2022-09-01 ] . (原始内容存档 于2022-09-02).
^ PyScript is an open source platform for Python in the browser . [2024-09-26 ] . (原始内容存档 于2025-01-18).
^ uvloop is a fast, drop-in replacement of the built-in asyncio event loop . [2023-03-24 ] . (原始内容存档 于2023-05-25).
^ Asynchronous HTTP Client/Server for asyncio and Python . [2021-01-14 ] . (原始内容存档 于2021-01-15).
^ PyGObject is a Python package which provides bindings for GObject based libraries such as GTK, GStreamer, WebKitGTK, GLib, GIO and many more . [2023-03-05 ] . (原始内容存档 于2023-06-07).
^ Remi - Python REMote Interface library . [2022-08-28 ] . (原始内容存档 于2022-08-28).
^ PySimpleGUI - Python GUIs for Humans . [2022-08-28 ] . (原始内容存档 于2022-08-28).
^ Gooey - Turn (almost) any Python command line program into a full GUI application with one line . [2022-08-28 ] . (原始内容存档 于2022-08-28).
^ Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies . [2023-03-18 ] . (原始内容存档 于2023-05-18).
^ pywebview is a lightweight cross-platform wrapper around a webview component that allows to display HTML content in its own native GUI window . [2023-03-18 ] . (原始内容存档 于2023-05-06).
^ NumPy Fundamentals and usage — Broadcasting . [2023-09-20 ] . (原始内容存档 于2022-07-03). The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.
^ CuPy: NumPy & SciPy for GPU . [2022-08-31 ] . (原始内容存档 于2022-08-31).
^ DataFrame . [2022-09-01 ] . (原始内容存档 于2022-09-01). DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.
^ Python tools for data visualization — High-level tools . [2023-09-19 ] . (原始内容存档 于2023-09-28).
^ Dask - Parallel computing with task scheduling . [2022-08-31 ] . (原始内容存档 于2022-08-31).
^ Python tools for data visualization . [2022-09-21 ] . (原始内容存档 于2024-05-04).
^ VisPy - interactive scientific visualization in Python . [2022-08-30 ] . (原始内容存档 于2022-07-13).
^ glumpy — Python+Numpy+OpenGL: fast, scalable and beautiful scientific visualization . [2023-09-18 ] . (原始内容存档 于2023-09-19).
^ seaborn: statistical data visualization . [2024-09-24 ] . (原始内容存档 于2020-12-03).
^ Vega-Altair: Declarative Visualization in Python . [2024-09-25 ] . (原始内容存档 于2025-02-02).
^ Bokeh — Interactive Data Visualization in the browser, from Python . [2023-09-19 ] . (原始内容存档 于2023-10-12).
^ plotly.py is an interactive, open-source, and browser-based graphing library for Python . [2024-09-24 ] . (原始内容存档 于2025-01-24).
^ Dash - Analytical Web Apps for Python, R, Julia, and Jupyter . [2022-09-21 ] . (原始内容存档 于2022-12-05).
^ Plotly.js - Open-source JavaScript charting library behind Plotly and Dash . [2022-09-21 ] . (原始内容存档 于2022-12-21).
^ Panel - A high-level app and dashboarding solution for Python . [2022-09-21 ] . (原始内容存档 于2023-01-16).
^ HoloViz — High-level tools to simplify visualization in Python . [2023-09-19 ] . (原始内容存档 于2023-10-11).
^ Streamlit — The fastest way to build data apps in Python . [2022-09-21 ] . (原始内容存档 于2023-01-17).
^ Voilà - Voilà turns Jupyter notebooks into standalone web applications . [2022-09-21 ] . (原始内容存档 于2022-12-31).
^ Joblib: running Python functions as pipeline jobs . [2022-09-19 ] . (原始内容存档 于2022-12-07).
^ Thread-pool Controls . [2022-09-19 ] . (原始内容存档 于2022-11-02).
^ TensorFlow API Documentation - Module: tf . [2022-08-31 ] . (原始内容存档 于2022-09-05).
^ TensorFlow API Documentation - Module: tf.keras . [2022-08-31 ] . (原始内容存档 于2020-01-22).
^ JAX - Composable transformations of Python+NumPy programs: differentiate, vectorize, JIT to GPU/TPU, and more . [2022-08-28 ] . (原始内容存档 于2021-02-05).
^ Autograd - Efficiently computes derivatives of numpy code . [2022-08-28 ] . (原始内容存档 于2022-07-18).
^ XLA: Optimizing Compiler for Machine Learning . [2022-08-28 ] . (原始内容存档 于2022-09-01).
^ PEG parser generator for Python . [2023-02-22 ] . (原始内容存档 于2023-03-05). Pablo Galindo Salgado. Guide to the Parser . [2023-02-22 ] . (原始内容存档 于2023-05-24). Bryan Ford. The Packrat Parsing and Parsing Expression Grammars Page . [2023-04-18 ] . (原始内容存档 于2023-06-02).
^ PeachPy - Portable Efficient Assembly Code-generator in Higher-level Python . [2023-04-04 ] . (原始内容存档 于2023-04-04).
^ llvmlite — A lightweight LLVM python binding for writing JIT compilers . [2024-09-21 ] . (原始内容存档 于2025-02-06).
^ SimPy — Discrete event simulation for Python . [2023-08-17 ] . (原始内容存档 于2016-12-12).
^ PyOpenGL is the most common cross platform Python binding to OpenGL and related APIs . [2022-01-07 ] . (原始内容存档 于2011-06-15).
^ ModernGL - Modern OpenGL binding for python . [2022-01-07 ] . (原始内容 存档于2022-04-15).
^ CUDA Python Manual . [2022-08-30 ] . (原始内容存档 于2022-08-30).
^ PyCUDA gives you easy, Pythonic access to Nvidia’s CUDA parallel computation API . [2022-05-29 ] . (原始内容存档 于2022-06-28).
^ PyOpenCL gives you easy, Pythonic access to the OpenCL parallel computation API . [2021-07-29 ] . (原始内容 存档于2022-03-08).
^ PySPH — A framework for Smoothed Particle Hydrodynamics in Python . [2023-12-26 ] . (原始内容存档 于2023-12-26).
^ Kompute: Blazing fast, mobile-enabled, asynchronous, and optimized for advanced GPU processing usecases . [2025-01-22 ] . (原始内容存档 于2025-01-24).
^ Taichi — Productive, portable, and performant GPU programming in Python . [2025-01-23 ] . (原始内容存档 于2025-01-09).
^ Graphene — GraphQL framework for Python . [2023-12-20 ] . (原始内容存档 于2024-04-28).
^ GQL — A GraphQL client in Python . [2023-12-25 ] . (原始内容存档 于2024-04-24).
^ Pillow: a modern fork of PIL . [December 8, 2013] . (原始内容 存档于2016-04-27).
^ pypdf - A pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files . [2023-02-24 ] . (原始内容存档 于2023-04-10).
^ PyFilesystem2 — Python's Filesystem abstraction layer . [2023-10-10 ] . (原始内容存档 于2023-10-13).
^ Fabric - Pythonic remote execution . [2013-04-08 ] . (原始内容存档 于2014-04-11).
^ Prefect - The easiest way to coordinate your dataflow . [2022-09-22 ] . (原始内容存档 于2023-01-11).
^ SDL Language Bindings . [2022-02-16 ] . (原始内容 存档于2019-03-12).
^ Arcade — Easy to use Python library for creating 2D arcade games . [2023-10-10 ] . (原始内容存档 于2023-11-08).
^ Pymunk is a easy-to-use pythonic 2d physics library that can be used whenever you need 2d rigid body physics from Python . [2024-09-24 ] . (原始内容存档 于2024-11-26).
^ Panda3D Manual . [2022-09-04 ] . (原始内容存档 于2018-12-29).
^ pythonnet - Python.NET . [2022-08-30 ] . (原始内容存档 于2022-09-01).
^ PyInstaller bundles a Python application and all its dependencies into a single package . [2021-03-13 ] . (原始内容 存档于2021-06-07).
^ LSB — Runtime Languages — Python Interpreter . [2023-06-16 ] . (原始内容存档 于2016-05-01).
^ pipx — Install and Run Python Applications in Isolated Environmentspipenv . [2023-05-07 ] . (原始内容存档 于2023-06-05).
^ XONSH is a Python-powered shell . [2022-08-28 ] . (原始内容存档 于2022-09-03).
^ Data Mining Fruitful and Fun - Open source machine learning and data visualization. Build data analysis workflows visually, with a large, diverse toolbox. . [2023-06-14 ] . (原始内容存档 于2023-06-06).
^ Mayavi: 3D scientific data visualization and plotting in Python . [2023-09-20 ] . (原始内容存档 于2023-10-11).
^ Ray - Effortlessly scale your most complex workloads . [2022-09-19 ] . (原始内容存档 于2022-12-13).
^ Kornia - Open Source Differentiable Computer Vision Library . [2022-09-22 ] . (原始内容存档 于2023-01-10).
^ spaCy: Industrial-strength NLP . [2023-09-22 ] . (原始内容存档 于2023-10-11).
^ Argos Translate — Open-source offline translation library written in Python . [2023-10-01 ] . (原始内容存档 于2023-11-13).
^ Generative Models by Stability AI . [2023-10-14 ] . (原始内容存档 于2023-11-11).
^ CLIP (Contrastive Language-Image Pretraining), Predict the most relevant text snippet given an image . [2023-10-18 ] . (原始内容存档 于2023-09-05).
^ Transformers — State-of-the-art Machine Learning for PyTorch, TensorFlow, and JAX . [2023-10-19 ] . (原始内容存档 于2023-09-27).
^ Gradio: The New Frontier in Interactive Python Data Apps . [2023-12-22 ] . (原始内容存档 于2023-12-22).
^ Python library for easily interacting with trained machine learning models https://pypi.org/project/gradio/ (页面存档备份 ,存于互联网档案馆 )
^ Buniatyan, Davit. Code Understanding Using LangChain . Activeloop. 2023 [2023-12-22 ] . (原始内容存档 于2023-12-16).
^ 270.0 270.1 TIOBE Index . TIOBE - The Software Quality Company. [2021-10-06 ] . (原始内容存档 于2020-06-14).
^ Python Discord is a large community focused around the Python programming language . [2021-01-13 ] . (原始内容存档 于2021-02-01).
^ Gotchas for Python Users . boo.codehaus.org. Codehaus Foundation. [2008-11-24 ] . (原始内容 存档于2008-12-11).
^ Esterbrook, Charles. Acknowledgements . cobra-language.com. Cobra Language. [2010-04-07 ] . (原始内容存档 于2008-02-08).
^ Coconut — Simple, elegant, Pythonic functional programming . [2024-09-27 ] . (原始内容存档 于2025-01-20).
^ Proposals: iterators and generators [ES4 Wiki] . wiki.ecmascript.org. [2008-11-24 ] . (原始内容 存档于2007-10-20).
^ Strachan, James. Groovy – the birth of a new dynamic language for the Java platform . 2003-08-29 [2007-06-11 ] . (原始内容 存档于2007-04-05).
^ Jalan, Nishant Aanjaney. Programming in Kotlin . CodeX. 2022-11-10 [2024-04-29 ] . (原始内容存档 于2024-11-30) (英语) .
^ The Mojo Programming Language . github.com. Modular. 2023 [2023-09-26 ] . (原始内容存档 于2023-10-25). Mojo is still young, but it is designed to become a superset of Python over time.
^ Yegulalp, Serdar. Nim language draws from best of Python, Rust, Go, and Lisp . InfoWorld. 2017-01-16 [2020-09-13 ] . (原始内容存档 于2018-10-13). Nim's syntax is strongly reminiscent of Python's, as it uses indented code blocks and some of the same syntax (such as the way if/elif/then/else blocks are constructed).
延伸閱讀
参閲
外部連接
可微分计算
概论 概念 应用 硬件 软件库
主题
分类