Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating Factory Functions

00:00 In this section, you’ll have a look at the number of use cases for closures in Python. So let’s start straight away with the first one. Sometimes you want to write functions to build closures that have some initial configuration or parameters.

00:14 This is particularly useful when you need to create multiple versions of similar functions. Let’s start with an example. You can work in a new script called factory_function.py.

00:28 Let’s create a make_root_calculator() function. And this function will create other functions that will allow you to work out the square root or the cube root or any other root of a number.

00:43 So this example is a math-focused example, but we all love math, right? So why not? So this function, make_root_calculator(), will accept one parameter, let’s call it root_degree.

00:55 So this will be two for the square root, three for the cube root, and so on. Now, this is not the function that will work out the square root or the cube root or any other root.

01:05 Instead, this outer function contains an inner function. You can call it root_calculator(). And this is the function that will actually perform the calculation.

01:15 So this function will accept some number and it’ll work out whichever root you need, the square root, the cube root, and so on. So this is the function that will return and to work out the root, you can use the built-in pow(), which allows you to raise number to a power.

01:32 So you can raise number to one divided by the root_degree.

01:38 So if the root_degree is two, you’re raising the power to one half, and that’s the same as the square root. I’ll let you convince yourself of the math outside of the course.

01:49 There’s one more thing you need to do. Since there’s an inner function root_calculator(), you need this inner function to be available in the main scope, in the global scope.

02:00 So the outer function must return root_calculator.

02:06 You don’t call root_calculator() since you’re only returning the name root_calculator, which refers to the inner function. So now you have the outer function make_root_calculator(), which is a factory function.

02:18 So this is a function that’s a factory for other functions. It’ll allow you to create root_calculator() functions. Let’s start with an example and then we’ll look back at the code to make sure we understand what’s happening.

02:30 You can create the square root function by calling, make_root_calculator() and passing in the value of two. So you would like square_root to be whatever, make_root_calculator(2) returns.

02:45 And this function will return the root_calculator() where root_degree is two. Let’s try it out. Let’s work out the square root of 49.

03:00 So you can run the script. And I called this script factory_function.py.

03:07 And the answer you get is 7.0 as you would expect.

03:11 But you can also create another function called cube_root, and you can use the same factory function, make_root_calculator(), but this time with the argument three for the cube root.

03:26 Let’s find a cube root of 125.

03:31 And there you can see we end up with a number very close to five. This is rounding error, of course, giving us 4.999 recurring.

03:38 So let’s see why you need closures for this to work. The parameter of the outer function root_degree, once you call the function and pass a value to it, that becomes a local variable within make_root_calculator().

03:53 Therefore, when the inner function root_calculator() refers to root_degree, it’s referring to a variable that was defined in the enclosing scope, and that’s the requirement for creating a closure.

04:06 The inner function is a closure that contains a reference to root_degree, the object created in the enclosing scope.

04:13 Therefore, the function square_root has access to root_degree, which is equal to two. However, the different function cube_root, it’s another instance of root_calculator(), but this instance has access to a root_degree, which is equal to three.

04:30 So square_root and cube_root are different instances of the same function. They are different closures created from the same function.

04:40 So in this lesson, you learned about one use case of closures, and these are factory functions. A factory function is a function you can use to create closures with specific values for certain parameters.

04:52 For example, in this lesson, you’ve created a make_root_calculator(), and this was the factory function. And you can use this factory function to create several instances of the root_calculator() function, each one with specific values for the root_degree.

05:07 So you have one factory function, which allows you to create as many functions as you need.

Become a Member to join the conversation.