Release: | 3.5.0b3 |
---|---|
Date: | July 07, 2015 |
This article explains the new features in Python 3.5, compared to 3.4.
For full details, see the Misc/NEWS file.
Note
Prerelease users should be aware that this document is currently in draft form. It will be updated substantially as Python 3.5 moves towards release, so it’s worth checking back even after reading earlier versions.
See also
PEP 478 - Python 3.5 Release Schedule
New syntax features:
New library modules:
New built-in features:
Implementation improvements:
Significantly Improved Library Modules:
Security improvements:
Please read on for a comprehensive list of user-facing changes.
The PEP added dedicated syntax for declaring coroutines, await expressions, new asynchronous async for and async with statements.
Example:
async def read_data(db):
async with db.transaction():
data = await db.fetch('SELECT ...')
PEP written and implemented by Yury Selivanov.
See also
PEP 492 – Coroutines with async and await syntax
This PEP proposes adding % formatting operations similar to Python 2’s str type to bytes and bytearray.
Examples:
>>> b'Hello %s!' % b'World'
b'Hello World!'
>>> b'x=%i y=%f' % (1, 2.5)
b'x=1 y=2.500000'
Unicode is not allowed for %s, but it is accepted by %a (equivalent of repr(obj).encode('ascii', 'backslashreplace')):
>>> b'Hello %s!' % 'World'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: %b requires bytes, or an object that implements __bytes__, not 'str'
>>> b'price: %a' % '10€'
b"price: '10\\u20ac'"
See also
PEP 461 – Adding % formatting to bytes and bytearray
This PEP proposes a new binary operator to be used for matrix multiplication, called @. (Mnemonic: @ is * for mATrices.)
See also
PEP 465 – A dedicated infix operator for matrix multiplication
PEP 471 adds a new directory iteration function, os.scandir(), to the standard library. Additionally, os.walk() is now implemented using os.scandir(), which speeds it up by 3-5 times on POSIX systems and by 7-20 times on Windows systems.
PEP and implementation written by Ben Hoyt with the help of Victor Stinner.
See also
PEP 471 – os.scandir() function – a better and faster directory iterator
PEP 475 adds support for automatic retry of system calls failing with EINTR: this means that user code doesn’t have to deal with EINTR or InterruptedError manually, and should make it more robust against asynchronous signal reception.
See also
PEP 475 – Retry system calls failing with EINTR
PEP 479 changes the behavior of generators: when a StopIteration exception is raised inside a generator, it is replaced with a RuntimeError. To enable the feature a __future__ import should be used:
from __future__ import generator_stop
Without a __future__ import, a PendingDeprecationWarning will be raised.
PEP written by Chris Angelico and Guido van Rossum. Implemented by Chris Angelico, Yury Selivanov and Nick Coghlan.
See also
PEP 479 – Change StopIteration handling inside generators
PEP 486 makes the Windows launcher (see PEP 397) aware of an active virtual environment. When the default interpreter would be used and the VIRTUAL_ENV environment variable is set, the interpreter in the virtual environment will be used.
See also
PEP 486 – Make the Python Launcher aware of virtual environments
PEP 488 does away with the concept of .pyo files. This means that .pyc files represent both unoptimized and optimized bytecode. To prevent the need to constantly regenerate bytecode files, .pyc files now have an optional opt- tag in their name when the bytecode is optimized. This has the side-effect of no more bytecode file name clashes when running under either -O or -OO. Consequently, bytecode files generated from -O, and -OO may now exist simultaneously. importlib.util.cache_from_source() has an updated API to help with this change.
See also
PEP 488 – Elimination of PYO files
PEP 489 updates extension module initialization to take advantage of the two step module loading mechanism introduced by PEP 451 in Python 3.4.
This change brings the import semantics of extension modules that opt-in to using the new mechanism much closer to those of Python source and bytecode modules, including the ability to use any valid identifier as a module name, rather than being restricted to ASCII.
See also
PEP 488 – Multi-phase extension module initialization
PEP 485 adds the math.isclose() and cmath.isclose() functions which tell whether two values are approximately equal or “close” to each other. Whether or not two values are considered close is determined according to given absolute and relative tolerances.
See also
PEP 485 – A function for testing approximate equality
Some smaller changes made to the core Python language are:
The new zipapp module (specified in PEP 441) provides an API and command line tool for creating executable Python Zip Applications, which were introduced in Python 2.6 in issue 1739468 but which were not well publicised, either at the time or since.
With the new module, bundling your application is as simple as putting all the files, including a __main__.py file, into a directory myapp and running:
$ python -m zipapp myapp
$ python myapp.pyz
You can now update docstrings produced by collections.namedtuple():
Point = namedtuple('Point', ['x', 'y'])
Point.__doc__ = 'ordered pair'
Point.x.__doc__ = 'abscissa'
Point.y.__doc__ = 'ordinate'
(Contributed by Berker Peksag in issue 24064.)
Since idlelib implements the IDLE shell and editor and is not intended for import by other programs, it gets improvements with every release. See Lib/idlelib/NEWS.txt for a cumulative list of changes since 3.4.0, as well as changes made in future 3.5.x releases. This file is also available from the IDLE Help -> About Idle dialog.
The following performance enhancements have been added:
Changes to Python’s build process and to the C API include:
async and await are not recommended to be used as variable, class or function names. Introduced by PEP 492 in Python 3.5, they will become proper keywords in Python 3.7.
The following obsolete and previously deprecated APIs and features have been removed:
This section lists previously described changes and other bugfixes that may require changes to your code.
The undocumented format member of the (non-public) PyMemoryViewObject structure has been removed.
All extensions relying on the relevant parts in memoryobject.h must be rebuilt.
The PyMemAllocator structure was renamed to PyMemAllocatorEx and a new calloc field was added.
Removed non-documented macro PyObject_REPR which leaked references. Use format character %R in PyUnicode_FromFormat()-like functions to format the repr() of the object.
Because the lack of the __module__ attribute breaks pickling and introspection, a deprecation warning now is raised for builtin type without the __module__ attribute. Would be an AttributeError in future. (issue 20204)
As part of PEP 492 implementation, tp_reserved slot of PyTypeObject was replaced with a tp_as_async slot. Refer to Coroutine Objects for new types, structures and functions.