đź§ł Python Nested Functions: A Function Within a Function
Let’s imagine you’re building a robot chef.
You give the robot a main function called cook_dinner()
. Inside that, there are little tasks: chop vegetables, boil water, stir the sauce. You don’t really want these small helper steps to be available for just anyone to call. They’re only useful inside the main cooking process.
So instead of defining them globally, you define them inside the main function. That, my friend, is what we call a nested function.
đź§Ş Example: The Talkative Function
def talk(phrase):
def say(word):
print(word)
words = phrase.split(' ')
for word in words:
say(word)
Here’s what’s going on:
- The outer function is
talk()
. It’s our main robot chef. - Inside it lives a helper function
say()
, whose only job is to print a single word. - We pass a whole phrase to
talk()
, and it splits it into individual words. - For each word, it calls the helper
say()
to speak it out loud.
Let’s run this:
talk("I love Python")
Output:
I
love
Python
Neat, isn’t it?
đź”’ The Magic of Privacy
Here’s something powerful about nested functions: they are private to the function in which they are defined.
Try this:
say("Hello") # Outside the function
And Python will complain:
NameError: name 'say' is not defined
Because say()
was born and raised inside talk()
, it doesn’t exist in the outside world. This makes your code cleaner, safer, and easier to manage.
đź§ Why Use Nested Functions?
- Encapsulation – You keep related logic together.
- Code clarity – You don’t pollute the global space with tiny helper functions.
- Closures – You can take advantage of inner functions remembering the outer function’s variables. (More on that in Chapter 33!)
🔄 Example 2: A Counter With Inner Increment
Here’s a practical one:
def count():
count = 0
def increment():
nonlocal count
count = count + 1
print(count)
increment()
Run count()
, and it prints 1
. Why? Because the inner increment()
function accessed and modified the outer count
variable using the nonlocal
keyword.
Without nonlocal
, it would treat count
as a new local variable, and the outer one would remain unchanged.
đź§ Summary
- A nested function is simply a function defined inside another function.
- It can only be used inside the outer function.
- It helps with organizing code and hiding logic that doesn’t need to be shared.
- You can access outer variables inside nested functions—especially powerful when combined with
nonlocal
.
📝 Practice & Review Time
-
In your own words, why might you want to use a nested function instead of a regular one?
-
What does the following code print?
def outer(): def inner(): print("Inside inner") inner() outer()
-
True or False: A nested function can be accessed from anywhere in your program.
-
Rewrite this using a nested helper function:
def greet(name): print("Hello, " + name + "!")
-
Add a nested function inside
multiply()
that prints"Multiplying!"
before returning the result:def multiply(a, b): # define nested function here return a * b