For more information on topics covered in this lesson, check out these resources:
Using Functions as Return Values: Closures
In this lesson, we’ll explore how and why you might use a function as a return value. In Python, functions are considered first-class objects. This means they can be assigned to a variable, used as an argument to a function, or used as the return value of a function. Here, specifically, this means that we can use a function object as the return value in any
return statement. A function that returns a function or takes a function as a parameter is referred to as a higher-order function.
00:52 A closure carries information about its enclosing execution scope. This provides a way for a function to keep track of its state information between function calls. Closure factory functions are useful when you need to write code based on the concept of lazy or delayed evaluation. As an example, suppose you want to write a function that takes a single number and provides the result of multiplying that number by a specific factor.
Say you always want to multiply a number by
2, or another function that always multiplies a number by
7, and so on. In this context, the value of the factor is going to be the same for a series of runs of this function. Here is a simple example, but it takes two arguments and doesn’t take into account that one of them,
factor, isn’t going to change much. Let’s do a little bit better.
This other function uses a closure to retain the value of
factor between function calls. Inside
by_factor() is an inner function called
multiply(), which is created and returned without being called.
The function object returned is a closure that retains information about the
factor being used. When you give a value for
by_factor() returns a function that knows to multiply its only parameter
number by the
factor provided as an argument when
by_factor() was called.
by_factor() returned for us a function that’s going to multiply a single parameter by
2. When we call
double() with an argument, it essentially calls
factor and whatever number was provided for the parameter
You can also use lambdas to create closures. This often provides a more concise version of your closure factory. For example, we just returned the lambda which takes the number and multiplies it by the argument provided in
by_factor() creates the function.
Again, I can create a
double() method, which calls
by_factor() with the argument of
2, and it returns a function that takes a
number and multiplies and returns that
number multiplied by this
2 we’re providing it.
04:56 For more information about inner functions, generators, lambdas, and anything else from this lesson, please check out the links below the video. Next, we’ll look at using decorators to write functions that take and return functions.
Become a Member to join the conversation.