Closure in Python and Javascript

Taohidul Islam
3 min readJan 9, 2019

--

Think of a function that remembers its variables/values after execution! Interesting, no? We can obtain this kind of advantages using “closure”.

Closure is one of the most important and often least understood concepts. Today, we shall explore some basics of closure.

At the beginning, we need to know about nested function.

Nested/Inner function

A function which is defined inside another function is known as nested or inner function. Nested functions are scoped inside their parent function.

Python Code:

Javascript Code:

Output :

Hi from PARENT function
Hi from CHILD function

In either case, we have defined a nested function named child_function and called the function inside parent function.

Nested functions are not directly accessible outside of parent function. If we want to try calling child_function outside of parent_function we will get errors.

Python : NameError: name ‘child_function’ is not defined

Javascript : Uncaught ReferenceError: child_function is not defined

As both Python and Javascript allow returning a function from another function then we can take its advantage.

Python Code :

Javascript Code :

Output : Hi from CHILD function

Here, we are returning reference of child_function from parent_function. And then using this reference we can call the inner function.

Closure

Closure is a function that remembers values/variables after execution.

Let, we want to write a “multiplier” using closure.

Python Code :

Javascript Code :

Output :

10
15

Here, at line 8 we are calling multiply_by function with argument 2 (factor=2). The wrapper function inherits argument (factor) from the outer function. multiply_by_2 stores the reference of wrapper function returned by multiply_by function.

At line 9, we’re calling multiply_by_2 with argument 2 (n = 5) and getting result 10 ( n*factor = 5*2=10). Here, is the twist! We’re not passing value of factor , multiply_by_2 function “remembers” previous value of factor (2).

We’ve done the same thing at line 11 and 12. We’re re-using our function.

Restriction

An inner function can refer the values of an outer function often seems perfectly normal when looking at the previous code. But here is a restriction that the inner function must be defined within the outer (parent) function, just passing in a function as an argument won’t work.

Such as, the following codes won’t work:

Python Code :

Javascript Code :

Python : NameError: name ‘factor’ is not defined

Javascript : Uncaught ReferenceError: factor is not defined

The following criteria need to be met to create closure :

  • We must have a nested/inner function
  • The inner function can refer to a value defined in the outer function.
  • The outer function must return the nested function.

When to use :

We can use closure to reuse our functions. When we have few functions in our code then closure is an efficient way. But if we need to have many functions then we should use class (Object oriented programming concept) .

You can know more about functions and decorators here :

  1. Functions as first class object in Python
  2. Understanding Python decorators
  3. Playing with inheritance in Python

You can also read :

  1. React navigation cheatsheet
  2. Min priority_queue in C++

This story is published in Noteworthy, where 10,000+ readers come every day to learn about the people & ideas shaping the products we love.

Follow our publication to see more product & design stories featured by the Journal team.

--

--