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

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
Download

Sample Code (.zip)

310.1 KB
Download

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.

Avatar image for g2sgautam

g2sgautam on June 29, 2021

Excellent course !!

Avatar image for lounap

lounap on July 3, 2021

Thank you! This was really helpful!

Avatar image for Jesus Espinoza

Jesus Espinoza on July 5, 2021

Great course!!! Really helpful!!!

Avatar image for jkueng

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.

Avatar image for Jon David

Jon David on Nov. 6, 2021

Fancy!

Avatar image for aniketbarphe

aniketbarphe on Nov. 28, 2021

Thank You!

Avatar image for elomari

elomari on March 19, 2023

Thank You!

Avatar image for sjr2

sjr2 on Aug. 16, 2023

Exceptional course! The examples you provided had the right amount of detail, and your explanations were very helpful. I was hoping you would include more examples showing classes containing __init__ methods with inner functions. I will write and test some programs that utilize class methods with inner functions to help me reinforce what I’ve learned. Thanks.

Become a Member to join the conversation.