days
1
3
hours
0
1
minutes
2
4
seconds
4
1
New release

Python 3.5 release arrives with new syntax features

Coman Hamilton

The final release for Python 3.5 brings users the advantage of new matrix multiplication operation, two new library modules and several built-in features.

The final release of Python 3.5 dropped late last week with many an improvement. Among the highlights of this release are three new syntax features.

Coroutines with Await and Async syntax: Python’s support for asynchronous programming has been improved with the addition of awaitable objects, coroutine functions, asynchronous iteration and asynchronous context managers.

Additional unpacking generalisations: Python will now allow uses of the * iterable unpacking operator and the ** dictionary unpacking operator, meaning that programmers to use an arbitrary number amount of unpackings in a function call. (No idea what that means? Read the details here)

Matrix multiplication operator: A common operation in science and mathematics fields, Python’s matrix multiplication now has a dedicated @ infix operator. The developer team have given the following example of a basic HTTP client using a coroutine function with the async def 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()

Python Zip Applications and Type Hints

On top of these changes, Python 3.5 has added two new library modules, zipapp and Type Hints. The zipapp module lets developers create executable Python Zip Applications using a dedicated command line tool and API. Meanwhile, the Type Hints has introduced a ‘provisional module’ to define the semantics of function annotations. In the Python Enhancement Proposal, Python creator Guido van Rossum said the goal of Type Hints was to “provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information.”

As well as security improvements and a new Windows installer, the latest release also boasts several built-in features:

  • bytes % args, bytearray % args: PEP 461 – Adding % formatting to bytes and bytearray.
  • b'\xf0\x9f\x90\x8d'.hex(), bytearray(b'\xf0\x9f\x90\x8d').hex(), memoryview(b'\xf0\x9f\x90\x8d').hex(): issue 9951 – A hexmethod has been added to bytes, bytearray, and memoryview.
  • memoryview now supports tuple indexing (including multi-dimensional).
  • Generators have a new gi_yieldfrom attribute, which returns the object being iterated by yield from expressions.
  • A new RecursionError exception is now raised when maximum recursion depth is reached.

As always, the download and full list of changes in all of its minute glory are on python.org.

Author
Coman Hamilton
Coman was Editor of JAXenter.com at S&S Media Group. He has a master's degree in cultural studies and has written and edited content for numerous news, tech and culture websites and magazines, as well as several ad agencies.

Comments
comments powered by Disqus