Python informer

Improve your Python coding skills

Closures

This article is part of a series on functional programming. Closures are a powerful and flexible way to create new functions out of existing functions. The Python implementation relies on inner functions. Inner functions An inner function is a function that is defined within the scope of another function, like this: def add_3(v): n = 3 def f(x): return x + n return f(v) print(add_3(6)) # Prints 9 In this case our outer function add_3 adds 3 to the value of x and returns the result. Read more →

Functional design patterns

This article is part of a series on functional programming. There are several design patterns that are often used with functional programming, although they are not all exclusive to the FP paradigm. Closures are a flexible way to create new functions from existing functions. They can be used to implement partial functions, composition and currying, among other uses. See also If you found this article useful you might be interested in my ebook Functional Programming in Python. Read more →

Functional Programming - contents

This section covers topics on functional programming (FP) in Python. Here is a list of the articles. Introduction Introduction to functional programming - an overview of functional programming in Python. Iterators Iterators - an introduction to iterators, iterables and lazy evaluation. Iterators vs iterables - the difference between an iterable and an iterator. Built-in functions on iterables - an overview of the functions built in to Python for transforming, reducing and converting iterables. Read more →

Functions

This article is part of a series on functional programming. Functions are a key part of functional programming. In this section we will look at what we mean by pure functions, how functions can be used as objects, and how lambda functions work. See also If you found this article useful you might be interested in my ebook Functional Programming in Python. Read more →

Pure functions

This article is part of a series on functional programming. Functional programming has the concept of pure functions. What are they, and why are they so useful? Mathematical functions Functional programming is based on the mathematical idea of functions. A mathematical function such as sin(x) simply returns a value. You give it an x value, it returns the value of the sine of x. If you give it the same x value you will always get the same answer. Read more →

Map/reduce example

This article is part of a series on functional programming. As an example of functional programming, in this article we will use some Python built in functions to analyse a text file. In the first example we will count the number of words in the file. In the second example we will extract the unique words. The basic pattern we will be using is map/reduce. In this pattern the map function is used to pre-process the input data. Read more →

Transforming iterables

This article is part of a series on functional programming. Higher order functions are functions that act on other functions. There are two built-in Python higher order functions - map and filter. map map applies a function to each element in an iterable, and returns the results as a new iterable. Here is an example: import math k = [1, 25, 81] e = map(math.sqrt, k) print(list(e)) #[1.0, 5.0, 9. Read more →

Built-in functions on iterables

This article is part of a series on functional programming. Python includes a number of built-in functions that work on iterables. These functions generally work with any iterable, either a lazy iterable (as returned by a generator or a range function), or a data structure such as a list or tuple. There are a few exceptions, noted in the descriptions below. Most of these functions are described below. The exceptions are: Read more →

Iterators vs iterables

This article is part of a series on functional programming. If you have studied Python even for a short while, you will probaly have come across the terms iterator, iterable and sequence. They are often used almost interchangeably, but they are all slightly different things. Iterators An iterator is very simple. It is any object that has a __next__ method. Each time you call the __next__ method, it will return a value. Read more →

Generators

This article is part of a series on functional programming. A generator is a convenient way to create your own iterators. Generator functions A generator function looks similar to a normal function, except that instead of a return statement it has a yield statement. This simple difference tells Python that it is a generator function. Here is an example: def count_down(): yield(5) yield(4) yield(3) yield(2) yield(1) You can use count_down anywhere you might use an iterator, for example in a for loop (it does a similar job to a range): Read more →