Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats new in Python 3.x?

Tags:

python

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?

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!". E.g. a function to make aliens invade, etc.

like image 919
Joe Avatar asked Oct 22 '09 17:10

Joe


2 Answers

Many of the completely new features introduced in 3.0 were also backported to 2.6, a deliberate choice. However, this was not practical in all cases, so some of the new features remained Python 3 - only.

How metaclasses work, is probably the biggest single new feature. The syntax is clearly better than 2.*'s __metaclass__ assignment...:

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.*. 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. 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.*.

Another syntax biggie is annotations -- see the PEP I'm pointing to for details. 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.

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.

like image 126
Alex Martelli Avatar answered Oct 17 '22 10:10

Alex Martelli


The section New Syntax lists, well, the new syntax in Python 3.x. I think it's debatable sometimes whether stuff is new or changed. E.g. exception chaining (PEP 3134): is that a new feature, or is it a change to the exception machinery?

In general, I recommend looking at all the PEPs listed in the document. They are the major changes/new features.

like image 30
Martin v. Löwis Avatar answered Oct 17 '22 11:10

Martin v. Löwis