In this lesson, you’ll see how to define Python functions inside of other functions.
Next, I want to show you inner functions. Is it possible to define functions inside of other functions? Sure! Let me show you an example. To start, I’ll have you define a function named
first_child(). So far, all of these functions don’t take any arguments. I made a small mistake there, pressing Return a little early, but it will work the same. So for this function,
first_child(), it will have a print statement saying
"Printing from the first_child() function".
And now I’ll have you define a
second_child() function. It will look similar,
"Printing from the second_child()". Now, before you leave the statement and end this function, I’ll have you call
second_child() with parentheses—so we’ll call it—and then call
Then it defines
first_child() and defines
second_child(), setting both of them up. Then, it actually will print
"the second_child() function", and print
"the first_child() function" last. I know this is a very simple example, but you can get the concept that even elaborate functions could be defined inside of other functions.
Become a Member to join the conversation.