Before learning the Python Closures, we must know the concept of nested function and non-local variables in Python.
Defining a function inside another function is called inner or nested function. Nested function can access the local variables of the enclosing scope.
def outerFunction(): var = "Hello" def innerFunction(): #using non-local variable var print(var) innerFunction() outerFunction()OUTPUT
As we can see that when we are calling outerFunction, innerFunction is executing and able to access the non-local variable var.
Non-Local Variable :
The local variable var of the outer function is a non-local variable for the inner function which it can access but not modify.
Example : try to modify the non-local variable in inner-function.
def outerFunction(): var = "Hello" def innerFunction(): var = "Hi" print(var) innerFunction() print(var) outerFunction()OUTPUT
As we can see in the above program, trying to modify the non-local variable var inside the innerFunction, but it is not getting modified. We can clearly see that after the execution of innerFunction, getting the the value of var as 'Hello'.
This happens due to variable scopes rule defined in the Python.
Now, if we want to modify the non-local variables inside the inner function then first we must declare them explicitly as non-local using nonlocal keyword.
def outerFunction(): var = "Hello" def innerFunction(): nonlocal var var = "Hi" print(var) innerFunction() print(var) outerFunction()OUTPUT
Another way to modify non-local variables, see the code below
def outerFunction(): outerFunction.var = "Hello" # declare the variable using function name def innerFunction(): outerFunction.var = "Hi" print(outerFunction.var) innerFunction() print(outerFunction.var) outerFunction()OUTPUT
What is Python Closure ?
A closure is an inner function that remembers it has access to variables in the local scopes in which its created, even after the outer function scope has been finished its executing.
def outerFunction(): var = "Hello" def innerFunction(): print(var) # Note we are returning function # Without parenthesis return innerFunction fun = outerFunction() fun()OUTPUT
The outerFunction() was called and returned function was referenced by the name 'fun'. On calling fun(), the var was still remembered although we had already finished executing the outerFunction() function.
Points to Remember about Closures
- There should be nested function i.e. function inside a function.
- The inner function must refer to a non-local variable or the local variable of the outer function.
- The outer function must return the inner function.
Next chapter is Anonymous Function
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at firstname.lastname@example.org
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at email@example.com