On December 23, 2016, the Python community released Python 3.6.0, the latest version of the popular programming language. The upcoming Fedora 26 release will include Python 3.6.0.

Users running Fedora Rawhide already enjoy Python 3.6. For those who want to experience the new Python features in Fedora 25 or Fedora 24, a python36 package is now available.

sudo dnf install python36

You can then invoke the new version’s shell with the command python36 or python3.6.

Python 3.6.0 (default, Jan 31 2017, 00:05:46) 
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.

Python 3.6 comes with many new enhancements and optimizations. This article gives a quick overview of what to expect. For a full list of new features check out the upstream docs.

Major changes

Formatted string literals (f-strings)

Python 3.6 offers compile time processing of format strings with the new f-string literals:

print(f"There were {len(docs)} found. First title: {docs[0].title}.")

These have dual benefits. The new literals are faster than runtime formatting, since the string gets broken up into text segments and field expressions at compile time. This results in zero runtime string parsing overhead. They are also easier to read, since you don’t need to mentally map expressions to their corresponding fields. They’re right there in the string. The literals are especially helpful for scripting use cases.

The order of elements in **kwargs

Keyword arguments now preserve their order. So collections.OrderedDict(first=1, second=2, third=2) finally works the way you would expect it to work. Previously the apparent key order in the source code would be lost in the process of calling the constructor.

Secrets module

The new secrets module provides handy helpers for secure token generation in various formats (e.g. bytes, hex strings, base64 strings) with a reasonable default amount of entropy.

Underscores in numeric literals

Underscores in numeric literals let you break up magic constants to make them easier to read. For instance, you can denote constants like 10_000_000.0, 0xCAFE_F00D, 0b_0011_1111_0100_1110.

File system path protocol

Many more standard library APIs, including the builtin open(), now support pathlib.Path and pathlib.PurePath objects through the new os.fspath() protocol. This change also means many third party libraries also indirectly gain support for these protocols, since they implicitly delegate the task of opening a path to a standard library API.

OpenSSL and hashlib

OpenSSL 1.1.0 is supported, along with additional hashing (BLAKE2, SHA-3, SHAKE) and key derivation (scrypt) algorithms.

Other notable changes

From a security perspective, os.urandom() now also provides a guarantee to either block or return a result suitable for cryptographic use. This means code that needs to run when the system entropy pool hasn’t been initialized yet should switch to use either:

  • the random module, if it doesn’t need cryptographic grade randomness, or
  • the new os.getrandom() API, to use the non-blocking variant of the syscall.

For folks using the new native async/await syntax for coroutine based service development, that syntax has been extended with provisional support for asynchronous comprehensions, generator definitions, and generation functions. This allows asynchronous code access to many more of the niceties developers are accustomed to when working with purely synchronous code.

For developers using mypy or one of the other type inference engines for Python, provisional support has been added for declarative variable annotations that allow inference engines to complain when values bound to the variable don’t abide by the expected constraint. The interpreter itself pays no attention to these annotations at runtime, just as it doesn’t check function annotations.

For developers writing internationalized applications, the Unicode database has been updated to 9.0.0.

For those debugging more complex applications, the new PYTHONMALLOC environment variable lets you either switch the runtime’s memory allocator into debug mode (PYTHONMALLOC=debug) or bypass it entirely (PYTHONMALLOC=malloc). Details can be found here.

Also related to application debugging, the -X tracemalloc option now provides a resource allocation traceback when printing ResourceWarning for resources that are cleaned up non-deterministically.

There have also been a range of performance improvement made to CPython. This change has been aided significantly by work on a new benchmarking utility (perf) and a new benchmark suite for Python interpreters (performance). Check out speed.python.org for 3.5 vs 3.6 performance data.

Try out Python 3.6

If you have a Python project, with a good set of tests of course, now is the right time to start running them in Python 3.6. Make use of the available Python testing tools like tox, to make sure your software runs correctly with different Python versions. For more information, refer to the Fedora Developer portal.