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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Python Inner Functions (Summary)

The use of inner functions in Python is a powerful tool allowing you to better encapsulate your code, create closures, and write decorators.

In this course, you learned how to:

  • Define inner functions
  • Use inner functions as helper functions
  • Build function closures
  • Use captured variables in a closure
  • Use captured data functions in a closure
  • Work with decorators


Sample Code (.zip)

310.1 KB


Course Slides (.pdf)

951.7 KB

00:00 In the previous lesson, I showed you how to use decorators with arguments. In this lesson, I’ll summarize the course.

00:07 This course covered the idea of defining functions inside of other functions, appropriately enough named inner functions. Inner functions have access to the scope of the function they are contained within. Functions declared inside another one are encapsulated, meaning they can’t be called from the global namespace.

00:26 Inner functions can be used as helper functions, where you’re doing a repetitive task within a containing function. Closures encapsulate data by using inner functions and returning references to them.

00:38 The encapsulated data is defined in the containing scope of the outer function. By instantiating a closure, you create something like an object. It has its own context.

00:49 You can parameterize multiple closures created off the same function. Each thing gets its own scoped context.

00:57 The captured variables inside of a closure can contain more than just data. They can contain reference functions as well. This kind of contained calling data function is called a decorator.

01:08 Python provides the @ operator to make wrapping functions with decorators easier than having to invoke closures. Decorators can be written to do things before and after the function they’re calling, giving a consistent way of implementing pre- and post-conditions. As decorators are closures and closures are functions, decorators—like functions—can take arguments.

01:31 The declaration of decorators with arguments takes an additional layer of inner functions. This makes writing a decorator that supports both regular and parameterized calling syntax a little more work. By using the callable function, you can determine what is being wrapped and write a decorator that can work in both fashions. Thanks for your attention!

01:52 I hope this course has been useful for you.

g2sgautam on June 29, 2021

Excellent course !!

lounap on July 3, 2021

Thank you! This was really helpful!

Jesus Espinoza on July 5, 2021

Great course!!! Really helpful!!!

jkueng on July 7, 2021

Very well done and insightful. The background of how inner functions are tied to functional programming really gave me context on why they are important.

Jon David on Nov. 6, 2021


aniketbarphe on Nov. 28, 2021

Thank You!

elomari on March 19, 2023

Thank You!

Become a Member to join the conversation.