How do nested functions work in Python?

Solution 1:

You are basically creating a closure.

In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be "closed over" its free variables.

Related reading: Closures: why are they so useful?

A closure is simply a more convenient way to give a function access to local state.

From http://docs.python.org/reference/compound_stmts.html:

Programmer’s note: Functions are first-class objects. A 'def' form executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details.

Solution 2:

You can see it as all the variables originating in the parent function being replaced by their actual value inside the child function. This way, there is no need to keep track of the scope of the parent function to make the child function run correctly.

See it as "dynamically creating a function".

def maker(n):
  def action(x):
    return x ** n
  return action

f = maker(2)
--> def action(x):
-->   return x ** 2

This is basic behavior in python, it does the same with multiple assignments.

a = 1
b = 2
a, b = b, a

Python reads this as

a, b = 2, 1

It basically inserts the values before doing anything with them.

Solution 3:

You are defining TWO functions. When you call

f = maker(2)

you're defining a function that returns twice the number, so

f(2) --> 4
f(3) --> 6

Then, you define ANOTHER DIFFERENT FUNCTION

g = maker(3)

that return three times the number

g(3) ---> 9

But they are TWO different functions, it's not the same function referenced, each one it's a independent one. Even in the scope inside the function 'maker' are called the same, is't not the same function, each time you call maker() you're defining a different function. It's like a local variable, each time you call the function takes the same name, but can contain different values. In this case, the variable 'action' contains a function (which can be different)

Solution 4:

That is what's called "closure". Simply put, for most if not all programming languages that treat functions as first-class object, any variables that are used within a function object are enclosed (i.e. remembered) so long as the function is still alive. It is a powerful concept if you know how to make use of it.

In your example, the nested action function uses variable n so it forms a closure around that variable and remembers it for later function calls.

Solution 5:

Let’s look at three common reasons for writing inner functions.

1. Closures and Factory Functions

The value in the enclosing scope is remembered even when the variable goes out of scope or the function itself is removed from the current namespace.

def print_msg(msg):
    """This is the outer enclosing function"""

    def printer():
        """This is the nested function"""
        print(msg)

    return printer  # this got changed

Now let's try calling this function.

>>> another = print_msg("Hello")
>>> another()
Hello

That's unusual. The print_msg() function was called with the string "Hello" and the returned function was bound to the name another. On calling another(), the message was still remembered although we had already finished executing the print_msg() function. This technique by which some data ("Hello") gets attached to the code is called closure in Python.

When To Use Closures?

So what are closures good for? Closures can avoid the use of global values and provides some form of data hiding. It can also provide an object oriented solution to the problem. When there are few methods (one method in most cases) to be implemented in a class, closures can provide an alternate and more elegant solutions. Reference

2. Encapsulation :

General concept of encapsulation is to hide and protect inner world from Outer one, Here inner functions can be accessed only inside the outer one and are protected from anything happening outside of the function.

3. Keepin’ it DRY

Perhaps you have a giant function that performs the same chunk of code in numerous places. For example, you might write a function which processes a file, and you want to accept either an open file object or a file name:

def process(file_name):
    def do_stuff(file_process):
        for line in file_process:
            print(line)
    if isinstance(file_name, str):
        with open(file_name, 'r') as f:
            do_stuff(f)
    else:
        do_stuff(file_name)

For more you can refer this blog.