Data Science Interview Prep: Q123
Python Closures. (Category: Python Programming)
Describe what a Python closure is, explain why it is useful, and provide an example.
Solution:
A Python closure is an inner function that remembers and can access variables from the outer function in which it was created, even after that outer function has finished executing.
Closures are useful because they let a function retain values from its outer function, allowing those values to be reused later and keeping related information together for clearer, easier-to-understand code.
Before we explore closures, let’s see how an inner function can access a variable defined in its enclosing outer function.
Explanation of the above code snippet:
We create an outer function ‘Georgia_Tech( )’, inside which we define a variable ‘major’ with the value
'Statistics'.Inside this outer function, we define an inner function ‘Industrial_and_Systems_Engineering( )’.
When the inner function runs, it accesses the enclosing scope of the outer function,(i.e., ‘Georgia_Tech( )’), retrieves the free (nonlocal) variable ‘major’, and prints its value.
A free (nonlocal) variable is a variable that is defined in an enclosing (outer) function, not inside the current function, but is still accessed by the inner function.
Let us now explore the concept of closures with a small modification to the code above.
Explanation of the above code snippet:
In this example, we define an outer function ‘Georgia_Tech_2( )’ with a variable ‘major’ set to 'Statistics'. Inside it, we define an inner function ‘Industrial_and_Systems_Engineering( )’.
Unlike the previous example, here we return the inner function itself (without parentheses) instead of executing it immediately. This allows the caller to invoke the inner function later, while it still has access to the free (nonlocal) variable ‘major’ from the outer function.
‘Georgia_Tech_2( )’ works similarly to ‘Georgia_Tech( )’, but the key difference is that it returns the inner function rather than running it right away, laying the groundwork for Python closures.
Explanation of the above code snippet:
Now, we assign the function ‘Georgia_Tech_2( )’ to the variable x. At this point, ‘x’ is itself a function — it points to the inner function ‘Industrial_and_Systems_Engineering’ that ‘Georgia_Tech_2( )’ returned, ready to be called later.
In Python, an object is any value or data item that has a type and exists in memory. Numbers, strings, lists, dictionaries, and even functions are all objects — in fact, everything in Python is an object.
A function object is a special kind of object that represents a function. It stores the function’s code along with its environment or scope, which allows the function to be assigned to variables, passed to other functions, or called later.
Explanation of the above code snippet:
When we print ‘x’, Python shows that it is a function object rather than executing it.
The address ‘0x1057efa60’ is the memory location where the Python object ‘x’ is stored.
The output confirms that ‘x’ refers to the inner function ‘Industrial_and_Systems_Engineering( )’ defined inside the outer function ‘Georgia_Tech_2( )’.
Explanation of the above code snippet:
‘x’ points to the inner function ‘Industrial_and_Systems_Engineering()’.
When we print ‘x.__name__’, Python returns the name of the function that x refers to, which in this case is ‘Industrial_and_Systems_Engineering’.
Explanation of the above code snippet:
The variable ‘x’ can be called like a function by adding parentheses at the end.
When we execute ‘x( )’, it runs the inner function ‘Industrial_and_Systems_Engineering( )’, which prints the value of the variable ‘major’, i.e., ‘Statistics’.
We can also demonstrate the closure concept when we pass parameters to the outer function.
Explanation of the above code snippet:
‘Georgia_Tech_3( )’ takes a parameter ‘mjr’ and assigns it to the variable ‘major’ in the outer function.
It returns the inner function ‘Industrial_and_Systems_Engineering( )’, which prints the value of ‘major’ when called.
For example, ‘Krishna( )’ prints ‘Statistics’ and ‘Shiva( )’ prints ‘Operations Research’, showing that each returned function remembers its own parameter.
We hope you found the article both enlightening and valuable! For more insightful content delivered straight to your inbox, simply enter your email address below and hit the subscribe button. Stay tuned for future updates!
References:
Python Closures: https://realpython.com/python-closure/








