资源描述
Python 语言参考手册Python ReferenceManualRelease 2.3.3Guido van RossumFred L Drake,Jr,editor 翻译团队:见文末名单译文最后修改:2004年2月18日PythonLabsEmail:docs python.orgPyt ho n is an in t erpret ed,o bjec t-o rien t ed,high-level pro grammin g lan guage wit h dyn amic seman t ic s.It s high-level built in dat a st ruc t ures,c o mbin ed wit h dyn amic t ypin g an d dyn amic bin din g,make it very at t rac t ive fo r rapid applic at io n develo pmen t,as well as fo r use as a sc ript in g o r glue lan guage t o c o n n ec t ex ist in g c o mpo n en t s t o get her.Pyt ho n s simple,easy t o learn syn t ax emphasizes readabilit y an d t herefo re reduc es t he c o st o f pro gram main t en an c e.Pyt ho n suppo rt s mo dules an d pac kages,whic h en c o urages pro gram mo dularit y an d c o de reuse.The Pyt ho n in t erpret er an d t he ex t en sive st an dard library are available in so urc e o r bin ary fo rm wit ho ut c harge fo r all majo r plat fo rms,an d c an be freely dist ribut ed.Pyt ho n是一种解释性的,面向对象的,具有动态语义的高级程序设计语言。它内建高级数据结构,配以 动态类型和动态捆绑,使其在快速应用开发中非常有利,就像脚本或粘合语言一样将已存在的构件连接 在一起。Pyt ho n的简单性和句法的易学性使其代码具有优秀的可读牲,因此维护程序的成本得以大大降 低。Pyt ho n具有模块和包的概念,以支持程序的模块化和代码重用。在主流平台上,Pyt ho n的解释器和 大量标准库都可以免费地以源代码形式或可执行文件形式获得,并且可以自由发布。This referen c e man ual desc ribes t he syn t ax an d“c o re seman t ic s o f t he lan guage.It is t erse,but at t empt s t o be ex ac t an d c o mplet e.The seman t ic s o f n o n-essen t ial built-in o bjec t t ypes an d o f t he built-in fun c t io n s an d mo dules are desc ribed in t he Python Library Reference.Fo r an in fo rmal in t ro duc t io n t o t he lan guage,see t he Python Tutorial.Fo r C o r C+pro grammers,t wo addit io n al man uals ex ist:Extending and Embedding the Python Interpreter desc ribes t he high-level pic t ure o f ho w t o writ e a Pyt ho n ex t en sio n mo dule,an d t he Python/C API Reference Manual desc ribes t he in t erfac es available t o C/C+pro grammers in det ail.本参考手册描述了该语言的语法和“核心语义”。手册本身是比较简洁的,但尽可能写得准确和完 整。那些非基本的内置对象类型、内置函数和模块的语义在Pyt ho n库参考中进行描述。对于语言 的浅显介绍,可以看看Pyt ho n入门手册.对于C和C+程序员,有两个文档可供参考:扩展和嵌 入Pyt ho n解释器是对Py由o n扩展模块设计的总体介绍;Pyt ho n/C API参考手册则向C/C+程序员们 细致地描述了可以使用的接口。目录第一章 简介 In t ro duc t io n 11.1 记法 No t at io n.1第二章词法分析Lex kalan aJysis 32.1 行结构Lin e st ruc t ure.32.2 其它语言符号Ot her t o ken s.72.3 标识符和关键字Iden t ifiers an d keywo rds.72.4 字面值Lit erals.82.5 运算符 Operat o rs.122.6 分隔符 Delimit ers.12第三章!!数据模型D at a m o del 153.1 对象值和类型Objec t s,values an d t ypes.153.2 标准类型层次The st an dard t ype hierarc hy.163.3 特殊方法名Spec ial met ho d n ames.26第四章 运行模型Ex ec ut io n m c del 414.1!代码块,运行结构框架和命名空间Namin g an d bin din g.414.2 异常Ex c ept io n s.42第五章 表达式Ex pressio n s 455.1 数值型间的转换Arit hmet ic c o n versio n s.455.2 原子 At o ms.465.3 基元 Primaries.485.4 嘉运算符The po wer o perat o r.535.5 一元算术运算符Un ary arit hmet ic o perat io n s.535.6 二元算术运算符Bin ary arit hmet ic o perat io n s.545.7 移位运算符Shift in g o perat io n s.555.8 二元位运算符Nn ary bit-wise o perat io n s.555.9 比较 Co mpariso n s.565.10 布尔运算Bo o lean o perat io n s.585.11 Lambda形式(lambda表达式)Lambdas.585.12 表达式表Ex pressio n list s.595.13 Evaluat io n o rder.595.14 总结Summary.59第六章 简单语句Sin pk st at an en t s 616.1 表达式语句Ex pressio n st at emen t s.616.2 断言语句Assert st at emen t s.626.3 赋值语句Assign men t st at emen t s.626.4 pass语句The pass st at emen t.656.5 del 语句The del st at emen t.666.6 prin t语句The prin t st at emen t.666.7 ret urn,语句The ret urn st at emen t.676.8 yield语句The yield st at emen t.676.9 raise语句The raise st at emen t.686.10 break语句The break st at emen t.696.11 c o n t in ue语句The c o n t in ue st at emen t.696.12 impo rt 语句The impo rt st at emen t.696.13 glo bal语句The glo bal st at emen t.726.14 ex ec 语句The ex ec st at emen t.72第七章 复合语句C o m po un d st at em eat s 757.1 if 语句The if st at emen t.767.2 while语句The while st at emen t.767.3 fo r语句The fo r st at emen t.777.4 t ry语句The t ry st at emen t.777.5 函数定义Fun c t io n defin it io n s.797.6 类定义Class defin it io n s.80第八章顶层构件To p 士vel8mpo n en t s 838.1 完整的Pyt ho n程序Co mplet e Pyt ho n pro grams.838.2 文件输入File in put.838.3 交互式输入In t erac t ive in put.848.4 表达式输入Ex pressio n in put.84附录A H dst o ry an d Lic en无 85A.l Hist o ry o f t he so ft ware.85A.2 Terms an d c o n dit io n s fo r ac c essin g o r o t herwise usin g Pyt ho n.86附录B修正记录 89附录C翻译团队 91第一早简 介 IntroductionThis referen c e man ual desc ribes t he Pyt ho n pro grammin g lan guage.It is n o t in t en ded as a t ut o rial.这个手册描述了Pyt ho n程序设计语言,本文档的目的不是入门.While I am t ryin g t o be as prec ise as po ssible,I c ho se t o use En glish rat her t han fo rmal spec ific at io n s fo r everyt hin g ex c ept syn t ax an d lex ic al an alysis.This sho uld make t he do c umen t mo re un derst an dable t o t he average reader,but will leave ro o m fo r ambiguit ies.Co n sequen t ly,if yo u were c o min g fro m Mars an d t ried t o re-implemen t Pyt ho n fro m t his do c umen t alo n e,yo u might have t o guess t hin gs an d in fac t yo u wo uld pro bably en d up implemen t in g quit e a differen t lan guage.On t he o t her han d,if yo u are usin g Pyt ho n an d wo n der what t he prec ise rules abo ut a part ic ular area o f t he lan guage are,yo u sho uld defin it ely be able t o fin d t hem here.If yo u wo uld like t o see a mo re fo rmal defin it io n o f t he lan guage,maybe yo u c o uld vo lun t eer yo ur t ime o r in ven t a c lo n in g mac hin e虽然我试图尽可能的精确,但还是选择了英语而不是形式化规范的方法描述,其中句法分析和词法分析部 分除外.这使的文档更具可读性,但却有可能存在歧义.因此,如果你来自火星并且想单单通过这篇文档就 重新实现Pyt ho n,你可能得猜测一些东西.事实上你很可能以实现一门完全不同的语言而告终;另一方面,如果你在使用Pyt ho n并且想了解其某个细节的精确规则,你可以在这里明确地得到它;如果你想获得更多 的语言形式化定义,可能就要自己搞了一-或者发明一个克隆机:-).It is dan gero us t o add t o o man y implemen t at io n det ails t o a lan guage referen c e do c umen t-t he implemen t at io n may c han ge,an d o t her implemen t at io n s o f t he same lan guage may wo rk differen t ly.On t he o t her han d,t here is c urren t ly o n ly o n e Pyt ho n implemen t at io n in widespread use(alt ho ugh a sec o n d o n e n o w ex ist s!),an d it s part ic ular quirks are so met imes wo rt h bein g men t io n ed,espec ially where t he implemen t at io n impo ses addit io n al limit at io n s.Therefo re,yo ull fin d sho rt(implemen t at io n n o t es“sprin kled t hro ugho ut t he t ex t.在语言参考文档中加入过多的实现细节是危险的事情-实现会改变,并且一个语言不同的实现可能以不 同的方式工作.另一方面,当前只有一个Pyt ho n的实现得到广泛使用(虽然已经存在了第二个实现).这里有 些细节还是被提及了,特别是某种实现增加了限制时,因此你可以在本文档中找到“实现注意”的标记.Every Pyt ho n implemen t at io n c o mes wit h a n umber o f built-in an d st an dard mo dules.These are n o t do c umen t ed here,but in t he separat e Python Library Reference do c umen t.A few built-in mo dules are men t io n ed when t hey in t erac t in a sign ific an t way wit h t he lan guage defin it io n.每个Pyt ho n实现都提供了大量的内建和标准模块,它们不在这个文档的介绍范围之内,但可以在另一个叫 做Pyt ho n库参考手册的文档中找到.少量与语言定义密切相关的内建模块也在这被介绍了.1.1 记法 NotationThe desc ript io n s o f lex ic al an alysis an d syn t ax use a mo dified BNF grammar n o t at io n.This uses t he fo llo win g st yle o f defin it io n:在描述词法和句法分析时候,我们使用不甚严格的BNF,通常是以下的定义方式:name:lc_letter(lc_letter|)*lc_letter:a.zThe first lin e says t hat a n ame is an lc _let t er fo llo wed by a sequen c e o f zero o r mo re lc _let t ers an d un dersc o res.An lc _let t er in t urn is an y o f t he sin gle c harac t ers a t hro ugh z.(This rule is ac t ually adhered t o fo r t he n ames defin ed in lex ic al an d grammar rules in t his do c umen t.)第一行说明n ame为lcet t er后跟随零个以上(包括零个)lcet t er或下划线的序列.lcet t er是,a“至“z中任意 一个字符.(实际上,这个”名字 的定义贯穿于本文档的整个词法和语法规则中)Eac h rule begin s wit h a n ame(whic h is t he n ame defin ed by t he rule)an d a c o lo n.A vert ic al bar(|)is used t o separat e alt ern at ives;it is t he least bin din g o perat o r in t his n o t at io n.A st ar(*)mean s zero o r mo re repet it io n s o f t he prec edin g it em;likewise,a plus(+)mean s o n e o r mo re repet it io n s,an d a phrase en c lo sed in square brac ket s()mean s zero o r o n e o c c urren c es(in o t her wo rds,t he en c lo sed phrase is o pt io n al).The*an d+o perat o rs bin d as t ight ly as po ssible;paren t heses are used fo r gro upin g.Lit eral st rin gs are en c lo sed in quo t es.Whit e spac e is o n ly mean in gful t o separat e t o ken s.Rules are n o rmally c o n t ain ed o n a sin gle lin e;rules wit h man y alt ern at ives may be fo rmat t ed alt ern at ively wit h eac h lin e aft er t he first begin n in g wit h a vert ic al bar.每个规则以一个名字(为所定义的规则的名字)和一个冒号为开始.竖线(一)用于分隔可选项.这是记法中结 合性最弱的符号.星号(*)意味着前一项的零次或多次的重复;同样,加号(+)意味着一次或多次的重复.在方 括号(口)中的内容意味着它可以出现零次或一次(也就是说它是可选的).星号和加号与前面的项尽可能地紧 密的结合,小括号用于分组.字符串的字面值用引号括住.空白字符仅仅在分隔语言符号(t o ken)时有用.通常 规则被包含在一行之中,有很多可选项的规则可能会被格式化成多行的形式,后续行都以一个竖线开始.In lex ic al defin it io n s(as t he ex ample abo ve),t wo mo re c o n ven t io n s are used:Two lit eral c harac t ers separat ed by t hree do t s mean a c ho ic e o f an y sin gle c harac t er in t he given(in c lusive)ran ge o f ASCII c harac t ers.A phrase bet ween an gular brac ket s()gives an in fo rmal desc ript io n o f t he symbo l defin ed;e.g.,t his c o uld be used t o desc ribe t he n o t io n o f c o n t ro l c harac t er,if n eeded.在词法定义中(如上例),有两个习惯比较常用:以三个句点分隔的一对串字面值意味着在给定(包括)的ASCII字符范围内任选一个字符。在尖括号()中的短语给出了非正式的说明,例如,这用在了需要说 明“控制字符”记法的时候.Even t ho ugh t he n o t at io n used is almo st t he same,t here is a big differen c e bet ween t he mean in g o f lex ic al an d syn t ac t ic defin it io n s:a lex ic al defin it io n o perat es o n t he in dividual c harac t ers o f t he in put so urc e,while a syn t ax defin it io n o perat es o n t he st ream o f t o ken s gen erat ed by t he lex ic al an alysis.All uses o f BNF in t he n ex t c hapt er(Lex ic al An alysis)are lex ic al defin it io n s;uses in subsequen t c hapt ers are syn t ac t ic defin it io n s.即使在句法和词法定义中使用的记号儿乎相同,但它们之间在含义上还是有着的很大不同:词法定义是 在输入源的一个个字符上进行操作,而句法定义是在由词法分析所生成的语言符号流上进行操作。在下 节(“词法分析”)中使用的BNF都是词法定义,以后的章节是句法定义.2第一章 1.简介mtroductionAA-第一早词 法分析 L exxalanaJysnsA Pyt ho n pro gram is read by a parser.In put t o t he parser is a st ream o f tokens,gen erat ed by t he lexical analyzer.This c hapt er desc ribes ho w t he lex ic al an alyzer breaks a file in t o t o ken s.一个Pyt ho n程序由解析器读入,输入解析器的是一个语言符号流,由词法分析器生成.本章讨论词法分析器 是如何把文件分隔成语言符号的.Pyt ho n uses t he 7-bit ASCII c harac t er set fo r pro gram t ex t.New in versio n 2.3:An en c o din g dec larat io n c an be used t o in dic at e t hat st rin g lit erals an d c o mmen t s use an en c o din g differen t fro m ASCII.Fo r c o mpat ibilit y wit h o lder versio n s,Pyt ho n o n ly warn s if it fin ds 8-bit c harac t ers;t ho se warn in gs sho uld be c o rrec t ed by eit her dec larin g an ex plic it en c o din g,o r usin g esc ape sequen c es if t ho se byt es are bin ary dat a,in st ead o f c harac t ers.Pyt ho n使用7比特长的ASCII字符集作为程序文本和串字面值.8比特长的字符的也可以作串字面值和注释,但它们的解释是依赖于平台的,在串中插入八比特字符的正确方法是使用八进制数和十六进制数的转义 字符.The run-t ime c harac t er set depen ds o n t he I/O devic es c o n n ec t ed t o t he pro gram but is gen erally a superset o f ASCII.运行时字符集依赖于连接到程序的I/O设备,但通常是ASCII的超集.Fut ure c o m pat bUiy n o t e:It may be t empt in g t o assume t hat t he c harac t er set fo r 8-bit c harac t ers is ISO Lat in-1(an ASCII superset t hat c o vers mo st west ern lan guages t hat use t he Lat in alphabet),but it is po ssible t hat in t he fut ure Un ic o de t ex t edit o rs will bec o me c o mmo n.These gen erally use t he UTF-8 en c o din g,whic h is also an ASCII superset,but wit h very differen t use fo r t he c harac t ers wit h o rdin als 128-255.While t here is n o c o n sen sus o n t his subjec t yet,it is un wise t o assume eit her Lat in-1 o r UTF-8,even t ho ugh t he c urren t implemen t at io n appears t o favo r Lat in-1.This applies bo t h t o t he so urc e c harac t er set an d t he run-t ime c harac t er set.向后兼容性备忘:假定8位字符集是ISO Lat in-1(一种ASCH码的超集,它覆盖了大部分使用拉丁字母的西 方语言.)看起来是个不错的做法,但是未来可能是支持Un ic o de的编辑器更流行一些,通常使用UTF-8(另一 种ASCH码的超集)编码,但是对于顺序在128到255之间的字符用法两者存在很大的区别。然而关于这点 还没有一致的意见,假定为Lat in-1或UTF-8都是不明智的,尽管当前的实现偏向于Lat in-1,这一点对于源 程序字符集和运行字符集都是适用的。2.1 行结构Line structureA Pyt ho n pro gram is divided in t o a n umber o f logical lines.一个Pyt ho n程序被分成多个逻辑行.2.1.1 逻辑行Logical linesThe en d o f a lo gic al lin e is represen t ed by t he t o ken NEWLINE.St at emen t s c an n o t c ro ss lo gic al lin e bo un daries ex c ept where NEWLINE is allo wed by t he syn t ax(e.g.,bet ween st at emen t s in c o mpo un d st at emen t s).A lo gic al lin e is c o n st ruc t ed fro m o n e o r mo re physical lines by fo llo win g t he ex plic it o r implic it line joining rules.3逻辑行以一个NEWLINE(新行)语言符号结束,语句不能跨多个逻辑行,除非语法上允许NEWLINE(例如,在复合语句的中的语句序列).一个逻辑行由一个物理行,或者以显式/隐式行连接规则连接的多个物理行 构成.2.1.2 物理行Physical linesA physic al lin e en ds in what ever t he c urren t plat fo rms c o n ven t io n is fo r t ermin at in g lin es.On Unix,t his is t he ASCII LF(lin efeed)c harac t er.On Win do ws,it is t he ASCII sequen c e CR LF(ret urn fo llo wed by lin efeed).On Mac in t o sh,it is t he ASCII CR(ret urn)c harac t er.一个物理行由所在平台的断行符号结束.在Un ix上,是ASCII LF(换行)字符;在DOS/Win do ws上,是ASCII字 符序列CR LF(回车加换行);在Mac in t o sh上,是ASCH CR(回车)字符.2.1.3 注释CommentsA c o mmen t st art s wit h a hash c harac t er(#)t hat is n o t part o f a st rin g lit eral,an d en ds at t he en d o f t he physic al lin e.A c o mmen t sign ifies t he en d o f t he lo gic al lin e un less t he implic it lin e jo in in g rules are in vo ked.Co mmen t s are ign o red by t he syn t ax;t hey are n o t t o ken s.一个注释以#字符(此时,它不能是串字面值的一部分)开始,结束于该物理行的结尾.如果没有隐式的 行连接,那么注释就意味着该逻辑行的终止.注释为句法分析所忽略,它们不记作语言符号.2.1.4 Encoding declarationsIf a c o mmen t in t he first o r sec o n d lin e o f t he Pyt ho n sc ript mat c hes t he regular ex pressio n c o din g =:s*(w-_.+)j,t his c o mmen t is pro c essed as an en c o din g dec larat io n;t he first gro up o f t his ex pressio n n ames t he en c o din g o f t he so urc e c o de file.The rec o mmen ded fo rms o f t his ex pressio n are#coding:whic h is rec o gn ized also by GNU Emac s,an d#vim:fileencoding=whic h is rec o gn ized by Bram Mo o len ars VIM.In addit io n,if t he first byt es o f t he file are t he UTF-8 byt e-o rder mark(,x efx bbx bff),t he dec lared file en c o din g is UTF-8(t his is suppo rt ed,amo n g o t hers,by Mic ro so ft s n o t ad).If an en c o din g is dec lared,t he en c o din g n ame must be rec o gn ized by Pyt ho n.The en c o din g is used fo r all lex ic al an alysis,in part ic ular t o fin d t he en d o f a st rin g,an d t o in t erpret t he c o n t en t s o f Un ic o de lit erals.St rin g lit erals are c o n vert ed t o Un ic o de fo r syn t ac t ic al an alysis,t hen c o n vert ed bac k t o t heir o rigin al en c o din g befo re in t erpret at io n st art s.The en c o din g dec larat io n must appear o n a lin e o f it s o wn.2.1.5 显式行连接Explicit line joiningTwo o r mo re physic al lin es may be jo in ed in t o lo gic al lin es usin g bac kslash c harac t ers(),as fo llo ws:when a physic al lin e en ds in a bac kslash t hat is n o t part o f a st rin g lit eral o r c o mmen t,it is jo in ed wit h t he fo llo win g fo rmin g a sin gle lo gic al lin e,delet in g t he bac kslash an d t he fo llo win g en
展开阅读全文