DEV Community

Cover image for CLOSURES
Abhinav Pasham
Abhinav Pasham

Posted on

CLOSURES

INTRODUCTION

While learning Python, I understood functions, nested functions, and scopes. But when I came across closures, I had one question: Why does Python even need closures? It felt like just another language feature until I understood the problem closures solve. In this article, I will try to make you understand better about closures.

What You Will Learn

  • Why closures Exist? -The problem they solve
  • What happens without closures
  • How closures work internally
  • Real-world use cases -How closures lead to decorators

Prerequisites

Before learning closures, you should understand:

-Functions
-Nested functions
-Variable scope (LEGB)
-Returning functions
-First-class functions

The Problem

Think, when u write the nested functions and u want to use the variable in the inner function which exists in the outer function after the outer finishes its execution so here we cannot access that variable.
Generally, the local scope disappears when the function finishes its execution.

  • Here to solve this problem it leads to the concept of closures.

What is closure?

A closure is a function that remembers and can access variables from its enclosing scope even after the enclosing function has finished execution.

def outer():
x = 10

def inner():
    print(x)

return inner
Enter fullscreen mode Exit fullscreen mode

func = outer()
func()
Explanation: As we saw in the above example, the outer function finishes its execution, but the inner function can still access the variable x. This is possible because of closures. Python preserves the variables from the enclosing scope that the inner function depends on, allowing it to access them even after the outer function has returned. This behavior follows the Enclosing scope in Python's LEGB rule.

How python achieves this?

`
outer()

Creates x

Creates inner()

inner references x

Python stores x along with inner

outer() finishes

inner still has x
`

EXAMPLE

def outer():
x = 10

def inner():
    print(x)

return inner
Enter fullscreen mode Exit fullscreen mode

func = outer()
func()
Explanation: Here initially the the function objects are initialized then the outer function is referenced and stored in the func variable here when u stored it it will be called and executes here it initializes x to 10 then it returns the inner function still here the inner function didnt called then when it returns and stored in the func now it is referring to the function inner then the func() is called then the inner funcition executes and it prints x here it will try to print the x while initially checking the local here the local variable is not present so it will check to the enclosing scope and prints it.

What if closures doesn't exist?

Without Closures:
outer()

Returns

x destroyed

inner()

NameError

Connection to decorators

A decorator works because
wrapper()

Creates inner()

inner remembers func

Returns inner
Without Clousures:
func()

would disappear

Decorators are built on top of closures.

Advantages of Closures

Examples:

Preserve state
Data hiding
Avoid global variables
Function factories
Callback functions
Foundation for decorators

Top comments (0)