Further Python Features#

Python provides much more features than we need. Here we list some of them, which might be of interest either because they simplify some coding tasks or because they frequently occur in other people’s code.

Doing Nothing With pass#

Python does not allow emtpy functions, classes, loops and so on. But there is a ‘do nothing’ command, the pass keyword.

def do_nothing():

    pass
    
    
do_nothing()

Checking Conditions With assert#

Especially for debugging purposes placing multiple if statements can be avoided by using assert instead. The condition following assert is evaluated. If the result is False an AssertionError is raised, else nothing happens. An optional error message is possible, too.

a = 0   # some number from somewhere (e.g., user input)

assert a != 0, 'Zero is not allowed!'
    
b = 1 / 0
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [2], in <cell line: 3>()
      1 a = 0   # some number from somewhere (e.g., user input)
----> 3 assert a != 0, 'Zero is not allowed!'
      5 b = 1 / 0

AssertionError: Zero is not allowed!

Structural Pattern Matching#

Python 3.10 introduces two new keywords: match and case. In the simplest case they can be used to replace if...elif...elif...else constructs for discrimating many cases. But in addition they provide a powerful pattern matching mechanism. See PEP 636 - Structural Pattern Matching: Tutorial for an introduction.

The set Type#

Python knows a data type for representing (mathematical) sets. Its name is set. Typical operations like unions and intersections of sets are supported. The official Python Tutorial shows basic usage.

Function Decorators#

Function decorators are syntactic suggar (that is, not really needed), but very common. They precede a function definition and consist of an @ character and a function name. Function decorators are used to modify a function by applying another function to it. The following two code cells are more or less equivalent:

def double(func):
    return lambda x: 2 * func(x)

@double
def calc_something(x):
    return x * x
def double(func):
    return lambda x: 2 * func(x)

def calc_something(x):
    return x * x
    
calc_something = double(calc_something)

See Function definitions in Python’s documention for details.

The copy Module#

The copy module provides functions for shallow and deep copying of objects. For discussion of the copy problem in the context of lists see Multiple Names and Copies.

Multitasking#

Reading in large data files or downloading data from some server may take a while. During this time the CPU is more or less idle and could do some heavy computations without slowing down data transfer. This a typical situation where one wants to have two Python programs or two parts of one and the same program running in parallel, possibly communicating with each other.

Python has the threading module for real multitasking on operating system level. The asynio module in combination with the async and await keywords provides a simpler multithreading approach completely controlled by the Python interpreter.

Graphical User Interfaces (GUIs)#

The Python ecosystem provides lots of packages for creating and controlling graphical user interfaces (GUIs). Here are some widely used ones:

  • tkinter in Python’s standard library provides support for classical desktop applications with a main window, subwindows, buttons, text fields, and so on.

  • ipywidgets is a very easy to use package for creating graphical user interfaces in Jupyter notebooks. For instance a slider widget could control some parameter of an algorithm.

  • flask is a package for building web apps with Python, that is, the user interacts via a website with the Python program running on a server.