From the course: Python: Decorators

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Functions within functions

Functions within functions - Python Tutorial

From the course: Python: Decorators

Functions within functions

- [Instructor] Now, if you define a function inside another function, then you're creating an inner function. Now, why would you want to do that? There are a couple of reasons for this. You can use them to provide encapsulations. This is when you need to protect or hide a given function from everything happening outside of it, so that function is totally hidden from the global scope. Now, one of the reasons why we're interested with working with functions within functions is because this is the basis for decorators. So let's kick things off with an example. So let's say I have a function called outer. And let's say outer accepts three arguments, a, b, and c, which are three Fibonacci numbers. And within this function, I want to create another function called wrapper, which is an inner function which doesn't take any arguments, but because these Fibonacci numbers are within its scope, it can return them. Now, functions are…

Contents