Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language 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 (Overview)

Python allows the declaration of functions inside of other functions. Inner functions, also known as nested functions, are defined within a function. This type of function has direct access to variables and names defined in the enclosing function in Python. Inner functions have many uses, most notably as closure factories and decorator functions.

In this course, you’ll learn 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 Welcome to Python Inner Functions. My name is Chris and I will be your guide.

00:06 Python supports the definition of functions inside of other functions. This allows you to do code encapsulation, closures, and decorators. One of Python’s features is its support for functional programming.

00:19 Functional programming is an alternate approach to structuring your code where everything is made up of functions, even the object-like things. Functional programming’s answer to objects are closures, functions with some state associated with them.

00:33 Closures allow the creation of decorators, which are handy way of doing pre- and post-conditions on a wrapped function. All these things require inner functions.

00:44 This course steps you through the basics of defining functions inside of functions, and then proceeds along a path leading to closures and decorators.

00:53 In this course, you will learn about defining inner functions, using inner functions as helper functions, building function closures, using captured variables in a closure, captured data functions in a closure, and decorators.

01:11 A quick note: Code in this course was tested in Python 3.9. Nothing here is particularly new. Most of it will even work in Python 2. I’ll do my best to point out any differences as I go along.

01:24 Python is considered a multi-paradigm language because it supports procedural, object-oriented, and functional programming styles. Functional programming has a deep history, starting with LISP in the 1960s.

01:35 This style of coding uses functions as first-class citizens and builds object-like things by encapsulating data within a function’s scope. This “function plus captured variable state” concept is called a closure.

01:48 To get a closure, you need nested, or inner, functions. Inner functions are functions defined within another function. It’s pretty cool that starting with something as simple as functions inside of functions, you can get to a place on par with object-oriented programming.

02:04 You can keep all sorts of data state inside of a closure, including references to other functions. Having closures on closures gives you a mechanism to associate pre- and post-conditions with a wrapped function. Python provides some syntactic sugar that reduces the amount of code you need to write when doing this.

02:21 This is called decorators. Enough chit-chat, let’s code! Next up, I’ll show you some inner functions.

Become a Member to join the conversation.