Python 3.9.0rc2 is out: Most exciting new features

Python 3.9.0rc2 is out: Most exciting new features

The latest release candidate for Python is now available. Jack Wallen highlights the best features of this iteration of the programming language.

Binary Code Vector Texture

Image: James Sanders/TechRepublic

Python is frequently listed as one of the most popular programming languages on the planet. It should be no surprise at how widespread its usage is, but very soon there’s going to be a dramatic change to the Python landscape, as Python 2 is deprecated. This is a challenge, given how much code out there depends on this particular iteration of Python. 

Developers have had fair warning. In fact, the Python developers even created a Python 2.7 end of life countdown clock, which now reads “0 months, 0 days, 0 hours, 0 minutes, and 0 seconds.”

In other words, the era of Python 2 is over–it will no longer be maintained after 2020. That means all developers who make use of Python must migrate to Python 3. This, of course, should be old news to most Python developers. What might not be old news is the release of Python 3.9.0rc2. 

SEE: Hiring kit: Python developer (TechRepublic Premium)

So long Windows 7

The 3.9.0rc2 release is the first version of Python to default to the 64-bit installer on the Windows platform. An important side effect of that is this release candidate will no longer install on unsupported Windows releases. In other words, unless you’re using Windows 10, you won’t be using Python 3.9.0rc2.

Outside of Python 3.9 no longer supporting any Windows release prior to 10, what are the exciting new features available for the language? Let’s first take a look at some of those included in the Python Enhancement Proposal (PEP) list for 3.9.0rc2.

PEP 584

This new proposal adds the union operators, | (merge) and |= (update) to the built-in dict class. This was done because the current method of merging two dicts carried a number of disadvantages. For example:

  • dict.update is not an expression and requires a temporary variable

  • {**d1, **d2} is not easily discoverable

  • collections.ChainMap resolves duplicate keys in the opposite expected order

  • dict(d1, **d2) only works when d2 is entirely string-keyed

To use Dict union the new dict must consist of a left operand merged with the right operand, each of which must be a dict. If a key is found in both operands, the last-seen value wins.

PEP 585

This change gets rid of the need for a parallel type hierarchy in the typing module. By doing so, it becomes easier for developers to annotate their works and easier to teach Python. PEP 585 avoids the existence of a duplicate collection hierarchy in the typing module. There are 38 collections that have become generic (using __class_getitem__()). That list includes:

With type annotations, you will now be able to use built-in collection types as generic types. 

PEP 616

This proposal adds two new methods to the APIs of Python’s various string objects. The methods are removeprefix() and removesuffix() and will remove either a prefix or suffix from a string which would then be added to:

  • Unicode str object

  • binary bytes

  • bytearray

  • collections.UserString

Adding these two methods can help make code:

  • Less fragile

  • More performant

  • More descriptive

PEP 617

The Python 3.9.0rc2 release includes a new PEG parser for CPython. This replaces the current LL(1)-based parser and is far more flexible than its predecessor. If your code requires using the old parser, you would have to use the command line switch -X oldparser

For a bit of background, parsing expression grammar (PEG) differs from context-free grammar as it is written in a way that more closely reflects how the parser operates when parsing the grammar. 

Although Python grammar technically falls under the guise of LL(1) parser, there are a number of rules that do not follow LL(1) guidelines, and therefore workarounds must be used in the grammar. 

The new PEG parser includes:

  • A parser generator capable of reading a grammar file and producing a PEG parser written in either Python or C that can parse the grammar.

  • A PEG meta-grammar that automatically generates a Python parser for the parser generator itself.

  • A generated parser capable of producing C and Python AST objects.

New modules

Python 3.9.0rc2 also includes two new modules to further expand the programming language. The new modules are:

  1. zoneinfo: This module adds support for the IANA time zone database to the standard library. This includes the addition of zoneinfo.ZoneInfo and a concrete datetime.tzinfo implementation that is backed by the time zone data of the system.
  2. graphlib: This adds the graphlib.TopologicalSorter class which includes the ability to perform topological sorting of graphs. A topological order is a linear ordering of the vertices in a graph such that u ≼ v whenever u is an ancestor of v.

Improved modules

A number of Python modules have been given considerable love in the rc2 release of 3.9.0. The list of modules that have been improved include:

  • ast: Added the ident option to dump()

  • asyncio: The reuse_address parameter of asyncio.loop.create_datagram_endpoint() is no longer supported

  • compileall:  Added the ability to use hardlinks for duplicated .pyc files

  • concurrent.features: Added the new cancel_futures parameter to concurrent.futures.Executor.shutdown() which cancels all pending futures instead of waiting for them to complete

  • curses: Added the curses.get_escdelay(), curses.set_escdelay(), curses.get_tabsize(), and curses.set_tabsize() functions

  • datetime: isocalendar() of datetime.data and isocalendar() of datetime.datetime now returns a namedtuple().

  • http: HTTP status codes 103, 418, and 425 have been added to http.HTTPStatus

  • random: Added a new random.Random.randbytes method.

  • smtplib: SMTP and SMTP_SSL now raises a ValueError if the given timeout for their constructor is zero

  • sys: Added the new sys.platlibdir attribute

  • tracemalloc: Added tracemalloc.reset_peak() to set the peak size of traced memory blocks to measure the peak of specific blocks of code

  • xml: White space characters within attributes are now preserved when serializing xml.etree.ElementTree to an XML file

Optimizations

With this new release, a few important optimizations have been made. Those optimizations are:

  • Optimized the idiom for the assignment of temporary variables in comprehensions

  • Optimized signal handling for multithreaded applications

  • Optimized the subprocess modules on FreeBSD

The list of new features and improvements for Python 3.9.0rc2 is quite significant. Make sure to check the entire list, so you know what’s in store for this new release.

Also see

Source of Article