简体   繁体   English

什么是Python 3.x的新功能?

[英]Whats new in Python 3.x?

http://docs.python.org/3.0/whatsnew/3.0.html says it lists whats new , but in my opinion, it only lists differences , so has does anybody know of any completely new Python features, introduced in release 3.x? http://docs.python.org/3.0/whatsnew/3.0.html说它列出了什么新的 ,但在我看来,它只列出了差异 ,所以有人知道任何全新的 Python功能,在第3版中引入。 X?

To Avoid Confusion, I will define a completely new feature as something that has never been used in any other code before, somehting you walk up to and go "Ooh, shiny!". 为了避免混淆,我将一个全新的功能定义为以前从未在任何其他代码中使用过的东西,有些你走过去然后去“噢,闪亮!”。 Eg a function to make aliens invade, etc. 例如,使外星人入侵的功能等。

Many of the completely new features introduced in 3.0 were also backported to 2.6, a deliberate choice. 3.0中引入的许多全新功能也被反向移植到2.6,这是一个深思熟虑的选择。 However, this was not practical in all cases, so some of the new features remained Python 3 - only. 但是,这在所有情况下都不实用,所以一些新功能仍然只是Python 3。

How metaclasses work, is probably the biggest single new feature. 元类如何工作,可能是最大的单一新功能。 The syntax is clearly better than 2.* 's __metaclass__ assignment...: 语法明显优于2.*__metaclass__赋值...:

class X(abase, metaclass=Y):

but more importantly, the new syntax means the compiler knows the metaclass to use before it processes the class body, and so the metaclass can finally influence the way the class body is processed -- this was not possible in 2.* . 但更重要的是,新语法意味着编译器在处理类主体之前知道要使用的元类,因此元类最终会影响类主体的处理方式 - 这在2.*是不可能的。 Specifically, the metaclass's new __prepare__ method can return any writable mapping, and if so then that's used instead of a regular dict to record the assignments (and assigning keywords such as def ) performed in the class body. 具体来说,元类的新__prepare__方法可以返回任何可写映射,如果是,则使用而不是常规dict来记录在类体中执行的赋值(以及分配诸如def之类的关键字)。 In particular, this lets the order of the class body finally get preserved exactly as it's written down, as well as allowing the metaclass, if it so chooses, to record multiple assignments/definitions for any name in the class body, rather than just the last assignment or definition performed for that name. 特别是,这使得类主体的顺序最终得到保存,就像它写下来一样,并允许元类(如果它选择的话)记录类主体中任何名称的多个赋值/定义,而不仅仅是为该名称执行的最后一次分配或定义。 This hugely broadens the applicability of classes with appropriate custom metaclasses, compared to what was feasible in 2.* . 2.*中可行的相比,这极大​​地拓宽了具有适当自定义元类的类的适用性。

Another syntax biggie is annotations -- see the PEP I'm pointing to for details. 另一种语法biggie是注释 - 请参阅我正指向的PEP以获取详细信息。 Python's standard library gives no special semantics to annotations, but exactly because of that third-party frameworks and tools are empowered to apply any semantics they wish -- such tasks as type-checking for function arguments are hereby allowed, though not directly performed by the standard Python library. Python的标准库没有为注释提供特殊的语义,但正是因为第三方框架和工具有权应用他们希望的任何语义 - 因此允许对函数参数进行类型检查等任务,尽管不是由标准的Python库。

There are of course many others (the new "views" concept embodied by such methods as dict's .keys &c in 3.* , keyword-only arguments, better sequence unpacking, nonlocal for more powerful closures, ...), of varying heft, but all pretty useful and well-designed. 当然还有很多其他的(新的“视图”概念通过dict的.keys &c在3.* ,仅关键字参数,更好的序列解包, nonlocal更强大的闭包,...)等方法体现,具有不同的重要性,但都非常有用和精心设计。

The section New Syntax lists, well, the new syntax in Python 3.x. 新语法部分列出了Python 3.x中的新语法。 I think it's debatable sometimes whether stuff is new or changed. 我认为有时候这些东西是新的还是改变的都是有争议的。 Eg exception chaining ( PEP 3134 ): is that a new feature, or is it a change to the exception machinery? 例外情况链接( PEP 3134 ):这是一个新功能,还是对异常机制的改变?

In general, I recommend looking at all the PEPs listed in the document. 一般来说,我建议查看文档中列出的所有PEP。 They are the major changes/new features. 它们是主要变化/新功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM