Monty Python 3.8 The Cycling Tour

A new version of Python was released on October 14, 2019, and it is exactly 17112 days or nearly 47 years after episode 3.8 (Season 3, Episode 8) of Monty Python’s Flying Circus. This technical article will go over some of the highlights of what is new in Python 3.8 that are interesting if you are an Engineer, Scientist or Quant using Python. For all details of what changed, see the Python 3.8.0 release notes.

Before diving into Python 3.8, it is interesting to see the release history of Python and how often it has changed.

Python Version Release Date Days
3.8.0 14 October 2019 474
3.7.0 27 June 2018 551
3.6.0 23 December 2016 467
3.5.0 13 September 2015 546
3.4.0 16 March 2014 533
3.3.0 29 September 2012 587
3.2.0 20 February 2011 603
3.1.0 27 June 2009 206
3.0.0 3 December 2008

It can be overwhelming to keep up to date with every aspect of what changed between Python 3.7 and Python 3.8. This is VersionBay’s list of main changes that is worthwhile knowing if you are an Engineer, Scientist or Quant.

  • New Features:
    • assignment expressions
    • positional-only parameters
    • f-strings support = for self-documenting expressions and debugging
  • Module Improvements:
    • cProfile

Assignment expressions

There is a new assignment operator in Python :=. It is also known as the walrus operator and allows you to reuse the assigned expression in a larger expression. Here is an example:

a = range(1, 12)
if (n := len(a)) > 10:
    print(f"List is too long ({n} elements, expected <= 10)")
# List is too long (11 elements, expected <= 10)

The idea behind this is to make code easier to read and avoid repetition of the instruction len(a), or the need to allocate n = len(a) before the if statement.

For more information please see: PEP 572.

Positional-only parameters

In Python 3.8 there is a new syntax using the / to define Positional-only parameters.  These parameters must be specified positionally and cannot be used as keyword arguments. If we look at this function definition:

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

We can see three different types of parameters:

  • a and b are positional-only parameters
  • c and d are positional or keyword parameters
  • e and f must be specified as keyword parameters

This means that one could use foo in the following ways:

# e and f must be keywords
foo(1, 2, 3, 4, e=5, f=6) 

# e and f must be keywords but their order can change
foo(1, 2, 3, 4, f=6, e=5)

# a and b are positional-only (new in 3.8) and cannot be specified as keyword
foo(1, 2, 3, d=4, e=5, f=6) # d is a keyword
foo(1, 2, d=4, c=3, e=5, f=6) # c and d are keywords (exchangeable order)
foo(1, 2, 3, 4, e=5, f=6) # c and d do not have to be keywords but are positional

# a and b are positional-only  (new in 3.8)
foo(1, 2, 3, 4, e=5, f=6)

For more information please see: PEP 570.

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

In Python 3.6 the concept of formatted strings was introduced. In Python 3.8 a new construct is added f'{exp=} with the intention of making it easier to debug. This enables the following:

import datetime


user = 'VersionBay'
member_since = datetime.date(2018, 11, 1)
print(f'{user=} {member_since=}') # f'{user=} is new.
# user='VersionBay' member_since=datetime.date(2018, 11, 1)

print(f'{user} {member_since}')
# VersionBay 2018-11-01

print(f'user={user} member_since={member_since}')
# user=VersionBay member_since=2018-11-01

For more information please see: PEP 498

cProfile

In Python 3.8 the cProfile.Profile class can now be used as a context manager. Enabling one to do the following:

def isPrime(n):
    for i in range(2, int(n**0.5) + 1):
        if n%i==0:
            return False

    return True

with cProfile.Profile() as profiler:
    # code to be profiled
    for idx in range(1, 10000):
        if isPrime(idx):
            print(idx)

profiler.print_stats()

This makes it much easier to profile part of code and not an entire script or module.

For more information please see: cProfile.Profile

If you found this article interesting and would like to learn more about Python Contact Us.