|
|
Log in / Subscribe / Register

Development

New features in Python 3.6

By Jake Edge
December 30, 2016

The Python 3.6 release occurred on December 23, only one week later than planned all the way back in October 2015. Python 3.6 adds a number of new features, including more support for asynchronous operations (generators and comprehensions), a filesystem path protocol, a new literal string formatting option, two random-number-related features, a frame evaluation API for debuggers and just-in-time (JIT) compilation, and more. Some of these features have been described in LWN articles along the way, but many haven't, so an overview of the highlights of the new release is in order.

As always, the release schedule (PEP 494) and, especially, the "What's New In Python 3.6" web pages provide an excellent overview of the release as well.

String formatting

Two string-formatting changes made their way into 3.6. The first adds yet another way to format text strings in Python using f-strings (which are described in PEP 498). This creates a new literal string type in the language (f'string') that can be used to directly interpolate variables (and full expressions) into strings:

    >>> num = 42
    >>> f'November 20{num}'
    November 2042
    >>> f'{num} * {num} = {num*num}'
    42 * 42 = 1764
The __format__() method of the bracketed expression is used to produce the value that gets interpolated into the string.

In addition, PEP 515 was adopted, which extends the language's syntax to allow underscores into numeric literals for readability purposes:

    a = 1_000_000
    b = 0x_abcd_ef09
The underscores are allowed for the benefit of humans reading the code, the parser simply ignores them.

A path protocol

The addition of a filesystem path protocol (PEP 519) will allow easier handling of path names in the language and standard library. The pathlib module was meant to help solve the problem of different representations for path names when it was added (on a provisional basis) for Python 3.4. But it suffered from a lack of widespread adoption, even by other parts of the language (e.g. open()), at least partly because it was provisional. Some discussions in the early part of this year led to the development of the PEP and the removal of pathlib's provisional status.

All of that paved the way for open() and the standard library to start accepting pathlib.Path objects. The protocol allows objects to declare their path-like nature by implementing the __fspath__() method. The os.PathLike() function can be consulted to determine if something is path-like and there are explicit operations in the os module to get either str or bytes representations of paths. The "What's New" page fills in some details as well:

The built-in open() function has been updated to accept os.PathLike objects, as have all relevant functions in the os and os.path modules, and most other functions and classes in the standard library. The os.DirEntry class and relevant classes in pathlib have also been updated to implement os.PathLike.

The hope is that updating the fundamental functions for operating on file system paths will lead to third-party code to implicitly support all path-like objects without any code changes, or at least very minimal ones (e.g. calling os.fspath() at the beginning of code before operating on a path-like object).

Frame evaluation

JITs were a hot topic at the 2016 Python Language Summit; one of the presentations on the Pyjion project introduced a plan to add an API to allow alternative ways to evaluate Python code (i.e. a frame-evaluation API). That idea has come to fruition with the adoption of PEP 523. It adds hooks to the CPython interpreter (the reference Python implementation) that will allow JIT engines as well as debugging tools to intercept the evaluation of frames. That will allow other programs to provide alternate ways to evaluate and execute the code in the frame objects.

Asynchronicity

Two PEPs that extend Python's asynchronous feature set have been adopted for 3.6. They both extend the async/await concept into more contexts. The addition of the async and await keywords in Python 3.5 furthered the move toward better support for asynchronous programming into the language itself. That largely started with the provisional adoption of the asyncio module back in 3.4 (see our coverage of Guido van Rossum's 2013 PyCon keynote for some background).

The asynchronous generators feature (as described in PEP 525) is effectively just a shortcut, but also provides improved performance—roughly twice as fast. Currently, in order to have an asynchronous generator that can be used in async for statements, a class must be defined that implements the __aiter__() and __anext__() asynchronous protocol. An alternative might have been to allow yield statements inside functions defined with async def, but when support for async/await was added, the yield statement was not allowed for async def coroutines. Changing the language to accept that made asynchronous generators easier to write and roughly twice as fast. The PEP gives an example of how that might be used (and compares it to how it would currently need to be written):

    async def ticker(delay, to):
	"""Yield numbers from 0 to `to` every `delay` seconds."""
	for i in range(to):
	    yield i
	    await asyncio.sleep(delay)

PEP 530 allows async and await to be available to comprehensions (i.e. list, dict, and set comprehensions as well as generator expressions) so that async can be used in coroutines defined with async def. The PEP gives an example of an asynchronous list comprehension that uses an asynchronous generator (agen()):

    [ i async for i in agen() ]
The list will be filled asynchronously (perhaps via something I/O-bound like a database lookup), while other parts of the program can still be running using the asyncio event loop.

In addition, await can be used in comprehensions in coroutines to wait for the completion of a asyncio.Future object. One of the dict examples given in the PEP is shown below, but more intricate comprehensions are possible:

    result = { fun : await fun() for fun in funcs }
The dictionary will be created with the elements of the funcs list as keys and values that come from awaiting the return values from calling those functions asynchronously.

Random numbers

As was described in an article back in July, Python will return to having a blocking os.urandom() call. That change was added to Python 3.5, but had an unexpected side effect: Python initialization could block waiting for enough entropy to initialize its seed for dictionary randomization (to avoid hash collision attacks). In 3.5.2, Python stopped using the blocking call for its own initialization, but also partly reverted the change to os.urandom() to give users some time to adjust. For Python 3.6 and beyond, though, the call will block until there is sufficient entropy (i.e. will call getrandom() without the GRND_NONBLOCK flag) in the kernel's entropy pool.

Another change that came about as the project looked at how Python generates random numbers is the advent of the secrets module, which has been specified to provide crypto-strength random numbers. The default random number generator provided by the random module is documented to be unsuited for cryptographic purposes, but was seen as an "attractive nuisance" (as PEP 506 called it) that would draw in new programmers based on code snippets on various web sites. It is hoped that adding secrets will provide users with an obvious standard library alternative that does not suffer from the shortcomings of random.

Variable annotations

Type annotations (or type hints) have been fast-tracked into Python over the last two years or so. The first support for the optional annotations was added to Python 3.5 (PEP 484), but that did not address annotations for variables (just function arguments and return values). PEP 484 suggested using comments as a stopgap, but it was clear that variable annotation would eventually come (see, for instance, Guido van Rossum's 2015 Python Language Summit talk on type hints).

As Van Rossum predicted in that talk, variable annotations will make an appearance in Python 3.6. The optional annotation will be allowed in variable initializations or just as a type declaration. From PEP 526:

    some_number: int           # variable without initial value
    some_list: List[int] = []  # variable with initial value

As with the earlier annotations, there are no language semantics associated with the annotations, but they will be placed into the __annotations__ ordered dictionary for the class, module, or function where they occur. They can also be retrieved using the typing.get_type_hints() API. It is important to note (and the PEP emphasizes) that: "Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention."

Compact dictionaries

The internal representation of the dict type has been changed to use far less memory (20-25% less compared to Python 3.5). This is a feature that came from the PyPy project. As a side effect of the new representation, dict now preserves the order in which items were added. This side effect should not be relied upon going forward, but choosing collections.OrderedDict when preserving the order is needed will benefit from the new dict implementation.

And more

There are more features listed for the release, including the removal of the provisional status for the asyncio module, updating the SSL modules to support OpenSSL 1.1.0, adding a PYTHONMALLOC environment variable for debugging memory allocation issues, and more. All in all, it is a solid release that brings some useful facilities, as well as rounding out some features that have appeared in earlier releases. It is far too early to guess what might appear in Python 3.7, but it is currently scheduled for a mid-2018 release.

Comments (9 posted)

Brief items

Development quote of the week

But how could a linter process that [web code], I asked - it was some unholy mess of 3(? maybe more) intermixed languages. It was gently explained that this was the source code form. A large tool chain would digest it, turning it into something no sane human would look at. It was broken into single language modules that were digestible by a browser, downloaded by some dynamic linker created by the tool chain that GET's the requisite parts as the running code links to it while executing. It was complete with debugging symbols packed into separate files, so they were there if needed. From the 1000' view it was not unlike the m4 / cpp / gcc / ld / ldd GNU tool chain - but created in some parallel universe.
Russell Stuart

Comments (none posted)

darktable 2.2.0 released

Darktable 2.2.0 has been released. This version includes the new automatic perspective correction module, a liquify tool for all your fancy pixel moving, a new image module to use a Color Look Up Table (CLUT) to change colors in the image, and much more.

Comments (2 posted)

Inkscape 0.92 released

Version 0.92 of the Inkscape vector drawing editor is available. "New features include mesh gradients, improved SVG2 and CSS3 support, new path effects, interactive smoothing for the pencil tool, a new Object dialog for directly managing all drawing elements, and much more. Infrastructural changes are also under way, including a switch to CMake from the venerable Autotools build system." See the release notes for details.

Comments (3 posted)

LedgerSMB 1.5.0 released

The LedgerSMB project has announced the release of version 1.5.0. "This release can be summarized as a major user experience boost through improvements in performance, look and stability. Performance improvements are being achieved by moving from per-request-invoked CGI-scripts to a preloaded-PSGI application as well as moving to a single-page web application using the Dojo toolkit."

Full Story (comments: none)

Python 3.6.0 released

The Python 3.6.0 release is out. Enhancements in this release include formatted string literals ("f-strings"), variable type annotations, asynchronous generators and comprehensions, and more; see the "what's new" document for details.

Full Story (comments: none)

sed-4.3 released [stable]

Jim Meyering has announced the release of sed-4.3. This release features much faster regular expression matching and faster I/O operations.

Full Story (comments: 2)

Newsletters and articles

Development newsletters

Comments (none posted)

Grumpy: Go running Python

The Google Open Source Blog introduces the Grumpy project. "Grumpy is an experimental Python runtime for Go. It translates Python code into Go programs, and those transpiled programs run seamlessly within the Go runtime. We needed to support a large existing Python codebase, so it was important to have a high degree of compatibility with CPython (quirks and all). The goal is for Grumpy to be a drop-in replacement runtime for any pure-Python project."

Comments (47 posted)

Ringing in 2017 with 90 hacker-friendly single board computers (HackerBoards)

HackerBoards.com takes a look at hacker-friendly single board computers. "Community backed, open spec single board computers running Linux and Android sit at the intersection between the commercial embedded market and the open source maker community. Hacker boards also play a key role in developing the Internet of Things devices that will increasingly dominate our technology economy in the coming years, from home automation devices to industrial equipment to drones. This year, we identified 90 boards that fit our relatively loose requirements for community-backed, open spec SBCs running Linux and/or Android."

Comments (25 posted)

Larsson: A stable base for Flatpak: 0.8

On his blog, Alexander Larsson reflects on the Flatpak 0.8 release and his plans for the application packaging and distribution format going forward. "My goal is to get the 0.8 series into the Debian 9 release, and as many other distributions as possible, so that people who create flatpaks can consider the features it supports as a reliable baseline. Sandboxing has always been one of the pillars of Flatpak, but even more important to me is cross-distro application distribution, even if not sandboxed. This is important because it gives upstream developers a way to directly interact with their users, without having an intermediate distributor. With 0.8 I think we have reached a level where the support for this is solid. So, if you ever thought about experimenting with Flatpak, now is the time!

Comments (7 posted)

Top open source creative tools in 2016 (opensource.com)

Over at opensource.com, Máirín Duffy has a lengthy overview of the open-source creative tools available. She covers the core applications (GIMP, Inkscape, Scribus, MyPaint, Blender, and Krita) for design, as well as tools for video, photography, 2D animation, audio, music, and more. "These six applications are the juggernauts of open source design tools. They are well-established, mature projects with full feature sets, stable releases, and active development communities. All six applications are cross-platform; each is available on Linux, OS X, and Windows, although in some cases the Linux versions are the most quickly updated. These applications are so widely known, I've also included highlights of the latest features available that you may have missed if you don't closely follow their development. If you'd like to follow new developments more closely, and perhaps even help out by testing the latest development versions of the first four of these applications—GIMP, Inkscape, Scribus, and MyPaint—you can install them easily on Linux using Flatpak."

Comments (7 posted)

Page editor: Rebecca Sobol
Next page: Announcements>>


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds