Matheus Portela

Python 3.5.0 has just been released

Last Sunday, the Python Software Foundation released the last update for the Python language. This afternoon, I finally had time to read the PEPs corresponding to the major new features and some of them are really exciting. Check out my compilation of nice features that Python 3.5.0 provides.

% formatting to bytes and bytearray

PEP 0461: In Python 3, bytes became a new type completely separated from str. Some areas of computer programming work constantly with mixed binary and ASCII data, such as in network and systems development, so this new formatting allows bytes and bytearray to use the same % formatting that exists for strings.

Attention: numeric codes are automatically encoded in ASCII format. For instance:

>>> b'%4x' % 10
b'   a'

>>> b'%04X' % 10

@ operator for matrix multiplication

PEP 0465: Numerical computation is an area where Python usage is growing day after day. In fact, packages such as numpy, scikit-learn, and theano are some of the most used Python packages, among standard and third-party packages.

Up to now, Python had only one multiplication operator - the star * - that has been used for two different purposes: element-wise and matrix multiplication. As we all know, duplication can lead to confusion and unreadable code, given that we always need to check how multiplication is implemented according to our data structures. Anyone who has written some numerical calculation in MATLAB realized that the offering of two multiplication operators avoid this confusion: * stands for vector and matrix multiplication and .* is element-wise multiplication.

Historically, Python packages used the * operator for one kind of multiplication and a dot method for the other one. However, complex expressions with a large number of matrix multiplications suffer from poor syntax and need to the be split in several intermediary steps in order to preserve its legibility, which is known as a code smell.

By introducing the @ operator for matrix multiplication, numerical calculations in Python have their legibility greatly improved. For instance, the following expressions, using numpy, are equivalent, except that the last one is much more readable.

>>> S = ( - r) - r)
>>> S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)

isclose(a, b)

PEP 0485: Still in the numeric computation realm, calculating whether a value is close enough to another value is extremely common. So common that a isclose method has just been added to the math module. This method also accepts a relative tolerance - the percentage in which a is considered to be close to b - or an absolute tolerance.

>>> x = 10
>>> y = 10.000001
>>> isclose(x, y, abs_tol=0.1)

coroutines with async and await

PEP 0492: Concurrent programming is huge and trending since the industry demands responsive and scalable code. Although Python introduced some coroutines capabilities earlier, they shared their implementation with common generator expressions, the only difference being that coroutines used the yield from expression. This was unclear and error prone.

In Python 3.5.0, coroutines are introduced as a standalone concept, with their own syntax and low-level implementation. PEP 0492 provides the following example of coroutine with the new syntax.

async def read_data(db):
    data = await db.fetch('SELECT ...')

These are my favorite features in Python 3.5.0 and I will definitely update my version just to check them out and play with coroutines and matrix multiplication.

Tell me, what features have caught your eyes?