Skip to main content


Association for Computing Machinery at the University of South Carolina

Functional Python


Justin Baum

This week's talk will be about functional programming in Python. Having the ability to work with multiple paradigms will make you a more rounded programmer. We'll cover the strengths of functional programming, and why you should do it. What and why lazy evaluation, comprehensions, filters, anonymous functions, and pattern matching may come in handy. Below are just some examples that at the end of the talk you should be able to incorporate into your code, responsibly hopefully. We'll also discuss the tradeoff of readability, complexity, and the functional paradigm. Transformations of data are the pillars behind functional programming, but done improperly can lead to cryptic code.

For more information about pattern matching being added to Python, see, PEP-622.

# Lazy Evaluation from itertools import count # in essence count is defined as: count = range(0, ∞) generator = (2**x for x in count()) # Comprehension powers_of_two = [2**x for x in count() for x in range(25)] # Anonymous functions anonymous = lambda x: x**3 print(anonymous(3)) print((lambda x: x**4)(3)) # Pattern Matching match response.status: case 200: do_something( # OK case 301 | 302: retry(response.location) # Redirect case 401: retry(auth=get_credentials()) # Login first case 426: sleep(DELAY) # Server is swamped, try after a bit retry() case _: raise RequestError("we couldn't get the data")