Change the future

Friday 5:10 p.m.–5:40 p.m.

Functional Programming with Python

Mike Müller

Audience level:
Intermediate
Category:
Best Practices/Patterns

Description

Python supports several functional programming concepts. The presentations shows how to use functional features such as functions as first-class objects, closures, side-effect-fee functions, currying, lazy evaluation, no mutable data structures and use of iterators instead of loops. The focus is on integration of these concepts in existing programs.

Abstract

Functional programming has been becoming more popular over the last few years. The often heard prejudice that this program paradigm is of mere academic interest has become much weaker. Haskell, Erlang and F# are commonly used functional languages that are applied in real-life applications.

Python supports several programming paradigms. In addition to the procedural and object oriented paradigms it also features import functional programming concepts. The presentations shows how to use functional features such as functions as first-class objects, closures, side-effect-fee functions, currying, lazy evaluation, no mutable data structures and use of iterators instead of loops. Examples help to demonstrate advantages and disadvantages for different types of tasks.

The structure of a program in functional style can be considerably different than that of an object printed program. Example show how to incorporate functional elements step-by-step into object oriented programs. The presentations shows how the itertools module of the standard library supports late evaluation. The functions of this module enable the programmer to write short, well-readable and efficient code for a variety of use cases. It also integrates very well in non-functional program parts.

Remarks

Each first-level bullet point corresponds to one idea and is typically one slide. Some might get two or three smaller/less dense slides.

The theoretical part is reduced to a minimum to give enough context. Listeners are assumed to have a basic understanding of functional programming. So the theory is just a very quick recap. Focus is on examples with source code. These code examples are often simplified to focus on the principles.

Remarks

Each first-level bullet point corresponds to one idea and is typically one slide. Some might get two or three smaller/less dense slides.

The theoretical part is reduced to a minimum to give enough context. Listeners are assumed to have a basic understanding of functional programming. So the theory is just a very quick recap. Focus is on examples with source code. These code examples are often simplified to focus on the principles.

Functional Programming with Python - Slide Outline

  • Introduction

    • The functional programming paradigm
    • Examples for functional languages
    • Industry use
  • Advantages of Functional Programming

    • No side effects --> robuster programs
    • Better testability
    • Focus on algorithms
  • Disadvantages of Functional Programming

    • Sometimes very different solutions than people are used to
    • Not suitable/best for all types of problems
    • IO does not really fit into the paradigm
    • Recursion is more complex than iteration
    • Immutability can be expensive
  • Functional Features in Python - Overview

    • Pure functions
    • Functions as objects
    • Closures and currying
    • Immutable data types
    • Lazy evaluation - aka generators
  • Example: Pure Functions

  • Example: Functions as Objects

  • Example: Closures and Currying

  • Example: Immutable Data Types

  • Example: Lazy Evaluation - Generators

  • Functional but not Pure

    • Combine with other paradigms - OOP and procedural
    • Don't make every problem look like a nail just because you have a hammer
    • Develop a feeling when to go functional (examples later)
  • Example: Avoid Side Effects

    • All attribute definitions in init
    • Use static methods to create attributes and call them in init
    • Reduction of side effects
    • Still classes and instances
  • Example: Freeze a Class

    • Allow mutable data structures only to a certain point
    • Freeze them == make attributes read-only
    • After freezing no more changes
    • Application: Growing a list during IO can be very convenient
    • When done building it, freeze it to avoid further, unintended changes
  • Example: Pipelining

    • Use generators to work with workflow-like problems
    • Parsing of log file
    • Pulling with yielding generators
    • Pushing with coroutines
  • Itertools - Lazy Programmers are Good Programmers

    • Quick overview of itertools module
    • Advantages
    • Disadvantages
    • Examples
  • Conclusion

    • Python has useful functional features
    • By no means a pure functional languages
    • Use the paradigm that is best for your objective
    • Combine functional style with other paradigms
    • Try to us more functional features when it is possible and improves your program
    • Stay pythonic, be pragmatic