Python 3.8 New Features - code name walrus

A brief and practical guide to the most important/interesting new features/changes coming with Python 3.8 (code name - walrus - joking :) ). So first lets announce the news:

Python 3.8 was released on October 14th, 2019.

The official documentation is here:

If you want to play with the new features without installing the latest python 3.8 then you can do it here: Python 3.8 (pre-release)

Brief video showing the new features

Overview

In this article we are going to have brief, high level review at some of the new features. Python 3.8 comes with a new warlus Operator, positional-only parameters and lots of other new and interesting changes, improvements and features.

It offers also another new features, plenty of language changes, one new module and improvements on others.

Many optimizations and C API changes can be found in the version. Several methods and commands are deprecated.

Personally I'll not move to python 3.8 soon. Nothing is so important or a game changer for the moment. Some features could be tricky or cause confusion which is another reason for me to stick to 3.7 for the moment.

New Features

For me the most interesting new features are 3:

  • The Walrus Operator
  • Positional-only parameters
  • f-strings support =

The Walrus Operator :=

In short this is a new operator which will be used as assignment or named expression. Official description from the PEP:

This is a proposal for creating a way to assign to variables within an expression using the notation NAME := expr.

If you wonder why it's called like this:

It is affectionately known as "the walrus operator" due to its resemblance to the eyes and tusks of a walrus.

The best way to understand the new operator is by viewing some examples like:

Avoid repeated expression calls:

num = 3
if (n := num % 2) != 0:
    print(f"Number is not even: number: {num}, remainder: {n}")

result:

Number is not even: number: 3, remainder: 1      

Regex expressions

import re, datetime
text = 'The latest version of python 3.8 is released on 14-10-2019 .. '

# before walrus
whole_match = re.findall(r'[\d]{1,2}-[\d]{1,2}-[\d]{4}', text)
if whole_match:
    print(whole_match)

# after walrus
if (found_date := re.findall(r'[\d]{1,2}-[\d]{1,2}-[\d]{4}', text)):
    print(found_date)

List comprehension

If a value computed in a filtering condition is also needed in the expression body:

numbers = [3, '0', 0, '6', 2, 1, 0, '7']
print([final_number for number in numbers if (final_number := int(number)) != 0])

result:

[3, 6, 2, 1, 7]

Errors and usage:

Sometimes walrus operator can be misleading and lead to confusion or errors. So it might be a good idea the usage to be limited to simple cases and expressions in order to avoid problems with code readability.

Below you can find list comprehension and walrus operator which leads to error:

numbers = [3, 0, 0, '6', 2, 1, 0, '7']
[number for number in numbers if (number := int(number)) != 0]

SyntaxError: assignment expression cannot rebind comprehension iteration variable 'number'

Another interesting case and possible abuse is:

if (I := he ) or ( you := me ) and ( we := alltogether):
    I = walrus

You can read more here: I walrus

Full description is available here: PEP 572

Positional-only parameters

New syntax for function parameters is introduced - /. This is an example function:

def f(a, /, b, *, c):
    print(a, b, c)

in this function parameter a is positional only and cannot be a keyword argument. While on the other hand c must be a keyword argument.

Let's check several examples:

correct call for this function:

f(10, 20, c=30)

result:

10 20 30
Errors

TypeError: f() takes 2 positional arguments but 3 were given

f(10, 20, 30) # c must be a keyword argument

result in

TypeError: f() takes 2 positional arguments but 3 were given

TypeError: f() got some positional-only arguments passed as keyword arguments: 'a'

f(a=10, b=20, c=30)   # a cannot be a keyword argument

result in

TypeError: f() got some positional-only arguments passed as keyword arguments: 'a'

Positional only parameters can be used in two ways. Their names are not shown as possible keywords which makes them available for kwargs:

def f(x, /, **kwargs):
    print(x, kwargs)

f(10, x=1, y=2)         # x is used in two ways
10 {'x': 1, 'y': 2}

For more info: Python Positional-Only Parameters

f-strings support = for self-documenting expressions and debugging

In 3.8 is added support for = for f-strings. So f-string like:

f'{version=} is released on {release_date=}'

will be represented as:

version='3.8' is released on release_date=datetime.date(2019, 10, 14)

Few more examples on this

from datetime import date

version = '3.8'
release_date = date(2019, 10, 14)
python_one_version = date(1994, 1, 1)

since_python_one = release_date - python_one_version

print(f'{version=} is released on {release_date=}')
print(f'{version=} day of Release {python_one_version.day=:02d}')
print(f'{version=} day of Release {since_python_one.days=:,d}')

results:

version='3.8' is released on release_date=datetime.date(2019, 10, 14)
version='3.8' day of Release python_one_version.day=01
version='3.8' day of Release since_python_one.days=9,

Used in previous python versions like python 3.7 will result in:

  File "<fstring>", line 1
    (version=)
            ^
SyntaxError: invalid syntax

The main idea is:

#(really) bad old days this was pretty wordy:
print "foo=", foo, "bar=", bar

#f-strings make this slightly nicer to type:
print(f"{foo=} {bar=}")

result:

foo=datetime.date(2019, 10, 14) bar=1

Other new features

Some of the features cannot be easily explained with examples so I'll just list them:

  • Parallel filesystem cache for compiled bytecode files
  • Debug build uses the same ABI as release build
  • PEP 578: Python Runtime Audit Hooks
  • PEP 587: Python Initialization Configuration
  • Vectorcall: a fast calling protocol for CPython
  • Pickle protocol 5 with out-of-band data buffers

Other language changes

In this section there are some quite interesting changes which might need your attention when you migrated from older python version to 3.8 like:

  • continue statement is now allowed in finally clause
  • new method as_integer_ratio() for bool, int and Fraction types:
x = 7
print(x.as_integer_ratio())

result:

(7, 1)
  • Added support of \N{name} escapes in regular expressions:
import re

notice = 'Copyright © 2019'
copyright_year_pattern = re.compile(r'\N{copyright sign}')
print(copyright_year_pattern.search(notice).group(0))

which will error in earlier versions:

sre_constants.error: bad escape \N at position 0

  • Dict and dictviews are now iterable in reversed insertion order using reversed()

  • Warnings when comma is missed in code like [(1, 2) (3, 4)]

before:

  File ".code.tio", line 1, in <module>
    [(1, 2) (3, 4)]
TypeError: 'tuple' object is not callable

now:

SyntaxWarning: 'tuple' object is not callable; perhaps you missed a comma?
  [(1, 2) (3, 4)]
Traceback (most recent call last):
  File ".code.tio", line 1, in <module>
    [(1, 2) (3, 4)]
TypeError: 'tuple' object is not callable
  • Arithmetic operations between subclasses of datetime.date or datetime.datetime and datetime.timedelta objects now return an instance of the subclass, rather than the base class.

For example:

from datetime import datetime, timezone

class DateTimeSubclass(datetime):
    pass

dt = DateTimeSubclass(2012, 1, 1)
dt2 = dt.astimezone(timezone.utc)

print(type(dt))
print(type(dt2))

in python 3.8

<class '__main__.DateTimeSubclass'>
<class '__main__.DateTimeSubclass'>

before:

<class '__main__.DateTimeSubclass'>
<class 'datetime.datetime'>
  • When the Python interpreter is interrupted by Ctrl-C (SIGINT) and the resulting KeyboardInterrupt exception is not caught, the Python process now exits via a SIGINT signal

  • Dict comprehensions have been synced-up with dict literals so that the key is computed first and the value second:

from unicodedata import normalize
names = ['Löwis', 'Łukasz', 'Dörwald']
print({(n := normalize('NFC', name)).casefold() : n for name in names})

result:

{'löwis': 'Löwis', 'łukasz': 'Łukasz', 'dörwald': 'Dörwald'}

Conclusion

Python 3.8 comes with new assignment operator, syntax for function parameters and many other improvements and features. At the first sight some of the changes look a bit controversial but they can offer new solution to old problems.

The examples covered in the article show potential for expanding the usage beyond the scope described in PEPs.

Once this version is well adopted I'll move to it in order to explore the full potential of it.

Previous python release is reviewed in this article: Python 3.7 features and release date