CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns


Closure In Python

A closure is created when a function is available inside another function.

When a function is available inside another function then closure is created. The inner function will have access to variable and parameters of outer function even after return of outer function. That means we can call the inner function later and it will have access to variables and parameters of outer function. The closure has reference to the variables and parameters of outer function.

Let's see some examples-

Example 1

A function is inside another function so closure is created and inside function has access to variable and parameter of outer function.





Output

Executing command : NetHub restore


Example 2

Closure will have reference to variable and parameter of outer function, not the actual value.





Output

Executing command : readELF dump


Example 3

For each call to a function a closure is created and value of variable and parameter will be applicable to that closure only.





Output

Executing command : NetHub 1.0 restore
Executing command : NetHub 2.0 restore
Executing command : NetHub 1.0 backup



Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his course on Advanced JavaScript.







Yearly Subscription


Advanced Python


Data Structures in Python


Design Patterns Through Python

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy