def maker(n):
def action(x):
return x ** n
return action
f = maker(2)
print(f)
print(f(3))
print(f(4))
g = maker(3)
print(g(3))
print(f(3)) # still remembers 2
Why does the nested function remember the first value 2
even though maker()
has returned and exited by the time action()
is called?
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 variablen
so it forms a closure around that variable and remembers it for later function calls.When you create a function with the def keyword, you are doing exactly that: you are creating a new function object and assigning it to a variable. In the code you gave you are assigning that new function object to a local variable called action.
WHen you call it a second time you are creating a second function object. So f points to the first function object (square-the-value) and g points to the second function object (cube-the-value). When Python sees "f(3)" it takes it to means "execute the function object pointed to be variable f and pass it the value 3". f and g and different function objects and so return different values.
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".
This is basic behavior in python, it does the same with multiple assignments.
Python reads this as
It basically inserts the values before doing anything with them.
Because at the time when you create the function,
n
was2
, so your function is:When you call f(3),
x
is set to3
, so your function will return3 ** 2
One use is to return a function that maintains a parameter.
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.
Now let's try calling this function.
That's unusual. The
When To Use Closures?print_msg()
function was called with the string"Hello"
and the returned function was bound to the nameanother
. On callinganother()
, the message was still remembered although we had already finished executing theprint_msg()
function. This technique by which some data ("Hello"
) gets attached to the code is called closure in Python.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:
For more you can refer this blog.