<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhinav Pasham</title>
    <description>The latest articles on DEV Community by Abhinav Pasham (@abhinav_pasham_d913ab013f).</description>
    <link>https://dev.to/abhinav_pasham_d913ab013f</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4039209%2F59716775-e83b-42e7-9dad-31cea88661ba.png</url>
      <title>DEV Community: Abhinav Pasham</title>
      <link>https://dev.to/abhinav_pasham_d913ab013f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhinav_pasham_d913ab013f"/>
    <language>en</language>
    <item>
      <title>CLOSURES</title>
      <dc:creator>Abhinav Pasham</dc:creator>
      <pubDate>Tue, 21 Jul 2026 06:01:27 +0000</pubDate>
      <link>https://dev.to/abhinav_pasham_d913ab013f/closures-1ch</link>
      <guid>https://dev.to/abhinav_pasham_d913ab013f/closures-1ch</guid>
      <description>&lt;h2&gt;
  
  
  INTRODUCTION
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Will Learn
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why closures Exist?
-The problem they solve&lt;/li&gt;
&lt;li&gt;What happens without closures&lt;/li&gt;
&lt;li&gt;How closures work internally&lt;/li&gt;
&lt;li&gt;Real-world use cases
-How closures lead to decorators&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before learning closures, you should understand:&lt;/p&gt;

&lt;p&gt;-Functions&lt;br&gt;
-Nested functions&lt;br&gt;
-Variable scope (LEGB)&lt;br&gt;
-Returning functions&lt;br&gt;
-First-class functions&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;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.&lt;br&gt;
Generally, the local scope disappears when the function finishes its execution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here to solve this problem it leads to the concept of closures.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is closure?
&lt;/h2&gt;

&lt;p&gt;A closure is a function that remembers and can access variables from its enclosing scope even after the enclosing function has finished execution.&lt;/p&gt;

&lt;p&gt;def outer():&lt;br&gt;
    x = 10&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def inner():
    print(x)

return inner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;func = outer()&lt;br&gt;
func()&lt;br&gt;
Explanation: As we saw in the above example, the outer function finishes its execution, but the inner function can still access the variable &lt;code&gt;x&lt;/code&gt;. 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  How python achieves this?
&lt;/h2&gt;

&lt;p&gt;`&lt;br&gt;
outer()&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Creates x&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Creates inner()&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;inner references x&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Python stores x along with inner&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;outer() finishes&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;inner still has x&lt;br&gt;
`&lt;/p&gt;

&lt;h2&gt;
  
  
  EXAMPLE
&lt;/h2&gt;

&lt;p&gt;def outer():&lt;br&gt;
    x = 10&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def inner():
    print(x)

return inner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;func = outer()&lt;br&gt;
func()&lt;br&gt;
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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if closures doesn't exist?
&lt;/h2&gt;

&lt;p&gt;Without Closures:&lt;br&gt;
outer()&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Returns&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;x destroyed&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;inner()&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;NameError&lt;/p&gt;

&lt;h2&gt;
  
  
  Connection to decorators
&lt;/h2&gt;

&lt;p&gt;A decorator works because&lt;br&gt;
wrapper()&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Creates inner()&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;inner remembers func&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Returns inner&lt;br&gt;
 Without Clousures:&lt;br&gt;
func()&lt;/p&gt;

&lt;p&gt;would disappear&lt;/p&gt;

&lt;p&gt;Decorators are built on top of closures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Closures
&lt;/h2&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;p&gt;Preserve state&lt;br&gt;
Data hiding&lt;br&gt;
Avoid global variables&lt;br&gt;
Function factories&lt;br&gt;
Callback functions&lt;br&gt;
Foundation for decorators&lt;/p&gt;

</description>
      <category>python</category>
    </item>
  </channel>
</rss>
