Skip to content
3.5.txt 77.7 KiB
Newer Older
Ryott Glayzer's avatar
Ryott Glayzer committed
What's New In Python 3.5
************************

Editors:
   Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov
   <yury@magic.io>

This article explains the new features in Python 3.5, compared to 3.4.
Python 3.5 was released on September 13, 2015.  See the changelog for
a full list of changes.

See also: **PEP 478** - Python 3.5 Release Schedule


Summary -- Release highlights
=============================

New syntax features:

* PEP 492, coroutines with async and await syntax.

* PEP 465, a new matrix multiplication operator: "a @ b".

* PEP 448, additional unpacking generalizations.

New library modules:

* "typing": PEP 484 -- Type Hints.

* "zipapp": PEP 441 Improving Python ZIP Application Support.

New built-in features:

* "bytes % args", "bytearray % args": PEP 461 -- Adding "%" formatting
  to bytes and bytearray.

* New "bytes.hex()", "bytearray.hex()" and "memoryview.hex()" methods.
  (Contributed by Arnon Yaari in bpo-9951.)

* "memoryview" now supports tuple indexing (including multi-
  dimensional). (Contributed by Antoine Pitrou in bpo-23632.)

* Generators have a new "gi_yieldfrom" attribute, which returns the
  object being iterated by "yield from" expressions. (Contributed by
  Benno Leslie and Yury Selivanov in bpo-24450.)

* A new "RecursionError" exception is now raised when maximum
  recursion depth is reached.  (Contributed by Georg Brandl in
  bpo-19235.)

CPython implementation improvements:

* When the "LC_TYPE" locale is the POSIX locale ("C" locale),
  "sys.stdin" and "sys.stdout" now use the "surrogateescape" error
  handler, instead of the "strict" error handler. (Contributed by
  Victor Stinner in bpo-19977.)

* ".pyo" files are no longer used and have been replaced by a more
  flexible scheme that includes the optimization level explicitly in
  ".pyc" name. (See PEP 488 overview.)

* Builtin and extension modules are now initialized in a multi-phase
  process, which is similar to how Python modules are loaded. (See PEP
  489 overview.)

Significant improvements in the standard library:

* "collections.OrderedDict" is now implemented in C, which makes it 4
  to 100 times faster.

* The "ssl" module gained support for Memory BIO, which decouples SSL
  protocol handling from network IO.

* The new "os.scandir()" function provides a better and significantly
  faster way of directory traversal.

* "functools.lru_cache()" has been mostly reimplemented in C, yielding
  much better performance.

* The new "subprocess.run()" function provides a streamlined way to
  run subprocesses.

* The "traceback" module has been significantly enhanced for improved
  performance and developer convenience.

Security improvements:

* SSLv3 is now disabled throughout the standard library. It can still
  be enabled by instantiating a "ssl.SSLContext" manually.  (See
  bpo-22638 for more details; this change was backported to CPython
  3.4 and 2.7.)

* HTTP cookie parsing is now stricter, in order to protect against
  potential injection attacks. (Contributed by Antoine Pitrou in
  bpo-22796.)

Windows improvements:

* A new installer for Windows has replaced the old MSI. See Using
  Python on Windows for more information.

* Windows builds now use Microsoft Visual C++ 14.0, and extension
  modules should use the same.

Please read on for a comprehensive list of user-facing changes,
including many other smaller improvements, CPython optimizations,
deprecations, and potential porting issues.


New Features
============


PEP 492 - Coroutines with async and await syntax
------------------------------------------------

**PEP 492** greatly improves support for asynchronous programming in
Python by adding *awaitable objects*, *coroutine functions*,
*asynchronous iteration*, and *asynchronous context managers*.

Coroutine functions are declared using the new "async def" syntax:

   >>> async def coro():
   ...     return 'spam'

Inside a coroutine function, the new "await" expression can be used to
suspend coroutine execution until the result is available.  Any object
can be *awaited*, as long as it implements the *awaitable* protocol by
defining the "__await__()" method.

PEP 492 also adds "async for" statement for convenient iteration over
asynchronous iterables.

An example of a rudimentary HTTP client written using the new syntax:

   import asyncio

   async def http_get(domain):
       reader, writer = await asyncio.open_connection(domain, 80)

       writer.write(b'\r\n'.join([
           b'GET / HTTP/1.1',
           b'Host: %b' % domain.encode('latin-1'),
           b'Connection: close',
           b'', b''
       ]))

       async for line in reader:
           print('>>>', line)

       writer.close()

   loop = asyncio.get_event_loop()
   try:
       loop.run_until_complete(http_get('example.com'))
   finally:
       loop.close()

Similarly to asynchronous iteration, there is a new syntax for
asynchronous context managers.  The following script:

   import asyncio

   async def coro(name, lock):
       print('coro {}: waiting for lock'.format(name))
       async with lock:
           print('coro {}: holding the lock'.format(name))
           await asyncio.sleep(1)
           print('coro {}: releasing the lock'.format(name))

   loop = asyncio.get_event_loop()
   lock = asyncio.Lock()
   coros = asyncio.gather(coro(1, lock), coro(2, lock))
   try:
       loop.run_until_complete(coros)
   finally:
       loop.close()

will output:

   coro 2: waiting for lock
   coro 2: holding the lock
   coro 1: waiting for lock
   coro 2: releasing the lock
   coro 1: holding the lock
   coro 1: releasing the lock

Note that both "async for" and "async with" can only be used inside a
coroutine function declared with "async def".

Coroutine functions are intended to be run inside a compatible event
loop, such as the asyncio loop.

Note:

  Changed in version 3.5.2: Starting with CPython 3.5.2, "__aiter__"
  can directly return *asynchronous iterators*.  Returning an
  *awaitable* object will result in a "PendingDeprecationWarning".See
  more details in the Asynchronous Iterators documentation section.

Loading
Loading full blame…