Python Closures: Encapsulating and Preserving Function State

thumb_up 1  ·  sell Python closures, Function state encapsulation in Python, Preserving state with closures in Python code

In this chapter, let us discuss the concept of closures in Python. In Python, functions are said to be first order objects. Just like the primary data types, functions can also be used assigned to variables, or passed as arguments.

Nested Functions

You can also have a nested declaration of functions, wherein a function is defined inside the body of another function.

Example

 
def functionA(): print ("Outer function") def functionB(): print ("Inner function") functionB() functionA()

It will produce the following output −

Outer function
Inner function

In the above example, functionB is defined inside functionA. Inner function is then called from inside the outer function's scope.

If the outer function receives any argument, it can be passed to the inner function.

 
def functionA(name): print ("Outer function") def functionB(): print ("Inner function") print ("Hi {}".format(name)) functionB() functionA("Python")

It will produce the following output −

Outer function
Inner function
Hi Python

What is a Closure?

A closure is a nested function which has access to a variable from an enclosing function that has finished its execution. Such a variable is not bound in the local scope. To use immutable variables (number or string), we have to use the nonlocal keyword.

The main advantage of Python closures is that we can help avoid the using global values and provide some form of data hiding. They are used in Python decorators.

Example

 
def functionA(name): name ="New name" def functionB(): print (name) return functionB myfunction = functionA("My name") myfunction()

It will produce the following output −

New name

In the above example, we have a functionA function, which creates and returns another function functionB. The nested functionB function is the closure.

The outer functionA function returns a functionB function and assigns it to the myfunction variable. Even if it has finished its execution. However, the printer closure still has access to the name variable.

nonlocal Keyword

In Python, nonlocal keyword allows a variable outside the local scope to be accessed. This is used in a closure to modify an immutable variable present in the scope of outer variable.

Example

 
def functionA(): counter =0 def functionB(): nonlocal counter counter+=1 return counter return functionB myfunction = functionA() retval = myfunction() print ("Counter:", retval) retval = myfunction() print ("Counter:", retval) retval = myfunction() print ("Counter:", retval)

It will produce the following output −

Counter: 1
Counter: 2
Counter: 3



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back