The closure is the function object which extends the scope of a function so that it can be invoked outside its scope.
A closure is unlike a plain function that allows the function to access those captured/free variables through the closure’s copies of their values, even when the function is invoked outside their scope.
All these will be more clear to you through examples.

Before diving into the example. Its good if you are known to “What is first class function?” and the properties of it which will be helpful in understanding the closure concept more easily.
If you are not known to go to this link -: blog.robofied.com/first-class-function/

In the last few blogs, we are following the example of company and office hours. So , continuing with these examples will try to understand the concept of closures with some changes in function.

Closures explaination through nested function

In [1]:

Comments inside the code will give you a better understanding of the concepts of closures.

## In this function we are returning the inner function from the outer function

def office_hours(start_time, finish_time):
s = start_time
f = finish_time
  def cal_working_hours():
return ('Hi '+ '! Your office hours will be ' + str((int(finish_time.split(':')[0]) - int(start_time.split(':')[0])))+ ' hours.')

## returning the inner function with () means function with execution
return cal_working_hours()

In [2]:

cal_hours = office_hours('10:00','18:00')

In [3]:

cal_hours

Out[3]:

'Hi ! Your office hours will be 8 hours.'

Here is the concept of closure comes

In [4]:

def office_hours(start_time, finish_time):
    s = start_time
    f = finish_time
    
    def cal_working_hours():
        return ('Hi '+ '! Your office hours will be ' + str((int(f.split(':')[0]) - int(s.split(':')[0])))+ 
              ' hours.')
    
    ## returning the function but without () 
    return cal_working_hours

In [5]:

## Here it will return only function name because () or execution of function didn't happen.
cal_hours = office_hours('10:00','18:00')
cal_hours

Out[5]:

<function __main__.office_hours.<locals>.cal_working_hours()>

In [6]:

## How to check the name of the funtion
cal_hours.__name__

Out[6]:

'cal_working_hours'

In [8]:

""" Now on that variable using "()" will be able to execute the function 
See here this cal_working_hours defined inside the office_hours function, and using the variables passed 
into the office hours still this cal_working_hours able to execute beacuse of closures """

cal_hours()

Out[8]:

'Hi ! Your office hours will be 8 hours.'

This example gave you the better understanding of the closures. With the help of closures, one can easily extend the scope of the inner function whixh is using variables set inside the outer function.

Stay tuned! Keep learning Python with us. We are aggregating all the important concepts of Python at one place. Keep enjoying these series.

Leave a Comment

Your email address will not be published. Required fields are marked *