Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Returning Functions From Functions

This lesson is from the Real Python video course by Christopher Bailey.

00:00 If functions can be defined inside of other functions, can a function also be returned from a function? Sure. It’s time for another example. Continuing in this idea of functions inside of functions, let me have you make a new one. This one will be named parent() also, so start off with def parent(). This time you’ll have an argument of numit will be used for selecting the child. Right away in the statement, you’re going to start with defining a new function named first_child() that doesn’t take any arguments, and for its statement, you’ll have it return a string of text that says "Hi, I am Emma".

00:41 Then, define a second_child() function. For that function, I’ll have you return text that says "Call me Liam". And last, before we return out of our main statement, returning from the function itself, we’ll set up a little condition.

00:56 If the argument num is equal to 1, return the function first_childnot calling it, but simply returning the function itself. else—in all other cases—return the other child, second_child. Again, not calling it, just simply returning it as a function. And close the statement.

01:15 So in this case, if you select parent, you’ll see that it’s a function. If you try to access first_child, it says that it’s not defined. Same with second_child.

01:29 But try this instead. Let’s set up a variable and let’s call it first. Into first, I’ll have you copy parent() with the number 1 in it.

01:40 So it’s calling the parent() function with the argument of 1.

01:45 That means if num is set to 1, it should return first_child. Okay. It looks like nothing happened. Well, let’s try a second one. Let’s set up another variable, second, and pass the argument of 2. So, what are these? We’ll look at first by itself—not call it, just type in the name and Return.

02:08 It says here that first is a function that lives inside the local scope of parent. Its name is first_child.

02:19 And then its memory location. What about second? second is a function. It lives inside parent, and second_child again is what it’s referencing inside of it. Can you call them?

02:35 Sure! If you call first() as a function. It prints out 'Hi, I am Emma'well, it returns the string 'Hi, I am Emma'. And second()very similar. It returns the string 'Call me Liam'.

02:50 So these two variables first and second are actual functions that are referencing—from the parent() function, inside of the scope of the parent()—the first_child function or the second_child function.

03:03 It’s referencing them.

03:08 And then they’re callable on the outside. Pretty neat! You’ve returned functions from within a function.

03:18 All right. I think you’re ready to talk about decorators, but first, let’s do a short review.

You must own this product to join the conversation.