PyConDE & PyData Berlin 2024

Functional Python
2024-04-23 , A05-A06

Python supports multiple programming paradigms. In addition to the procedural and object-oriented approach, it also provides some features that are typical for functional programming.

While these features are optional, they can be useful to create better Python programs. This tutorial introduces Python features that help to implement parts of Python programs in the functional style. Objective is not to write pure functional programs but improve programs design by using functional feature where suitable.

The tutorial points out advantages and disadvantages of functional programming in general and in Python in particular. Participants will learn alternative ways to solve problems. This will broaden their programming toolbox.


Audience

Intermediate Python programmers who like to learn more about functional programming and its application Python.

Format

The tutorial will be hands-on. I will use JupyterLab and will start with an empty Jupyter Notebook. I will unroll the tutorial content by typing. In addition, I will distribute scripts before the tutorial to avid too lengthly typing. I will load these scripts one by one into a Notebook.

Participants will have the opportunity to type along. I am a rather slow typer. In addition, I will stop typing often to explain. This gives most participants plenty of time to follow along. The PDF handout is very comprehensive and contains most of what I type. This allows students to pick if they should fall behind.

Outline

  • Functional programming basics (10 min)
    • Overview programming paradigms
    • Features of functional programming
    • Advantages of functional programming
    • Disadvantages of functional programming
    • Python's functional features - overview
  • Pure functions (5 min)
  • Callables and functions in Python (20 min)
    • Callables
    • Closures
    • "Currying"
    • Partial functions
    • Recursion
    • Lambda
    • Single Dispatch
  • No Loops - map, filter, and reduce (10 min)
    • Processing iterables with map
    • Select from iterables with filter
    • Reductions of iterables with reduce
  • Operators as Functions (10 min)
    • Arithmetic operators
    • Logical operators
    • Attribute access
    • Lookup
  • Comprehensions (15 min)
    • Simple
    • Nested
    • Dictionary comprehensions
    • Set comprehensions
  • Iterators (15 min)
    • Itertools
      • Infinite iterators
      • Iterators terminating on the shortest input sequence
      • Combinatoric iterators
  • External tools (5 min)
    • More itertools
    • Toolz

Expected audience expertise: Domain:

Novice

Expected audience expertise: Python:

Intermediate

Abstract as a tweet (X) or toot (Mastodon):

Learn how to integrate functional features into your daily Python programming.

I've been a Python user since 1999, teaching Python professionally since 2004.
I am also active in the community, organizing Python conferences such as
PyCon DE, EuroSciPy, and BarCamps.
I am a PSF Fellow and chair of the German Python Software Verband.