Copyright Intellipaat. All rights reserved.
Closures in Python
Copyright Intellipaat. All rights reserved.
Agenda
03 Example
01 Prerequisite
04 Use case
02 Definition
Copyright Intellipaat. All rights reserved.
Prerequisite
Copyright Intellipaat. All rights reserved.
Prerequisite
First class function
First class functions allows us to treat functions as any other objects, so that we can use them to:
 pass as an argument to another function
 return the function from another function
 assign the function to another variable
Nested function
Python supports the concept of a "nested function" or "inner function", which is simply a function
defined inside another function.
Note: Not to be confused with recursive functions
Copyright Intellipaat. All rights reserved.
Definition
Copyright Intellipaat. All rights reserved.
Definition
Closure is an inner function that remembers and has access to the variables in the local scope in which
it was created even after the outer function has finished executing.
A closure closes over their free variable from their environment
Copyright Intellipaat. All rights reserved.
Example
Copyright Intellipaat. All rights reserved.
Use case
Copyright Intellipaat. All rights reserved.
Use case
 To replace the unnecessary use of class: Suppose you have a class that contains just one method
besides the __init__ method. In such cases, it is better to use a closure instead of a class.
 To avoid the use of the global scope: If you have global variables which only one function in your
program will use, think closure. Define the variables in the outer function and use them in the inner
function.
 To implement data hiding: The only way to access the enclosed function is by calling the enclosing
function. There is no way to access the inner function directly.
 To remember a function environment even after it completes its execution: You can then access the
variables of this environment later in your program.
Copyright Intellipaat. All rights reserved.
• Further Learning..
Copyright Intellipaat. All rights reserved.
Further Learning
Copyright Intellipaat. All rights reserved.
India: +91-7022374614
US: 1-800-216-8930 (TOLL FREE)
support@intellipaat.com
24/7 Chat with Our Course Advisor

Python Closures Explained | What are Closures in Python | Python Closures

  • 1.
    Copyright Intellipaat. Allrights reserved. Closures in Python
  • 2.
    Copyright Intellipaat. Allrights reserved. Agenda 03 Example 01 Prerequisite 04 Use case 02 Definition
  • 3.
    Copyright Intellipaat. Allrights reserved. Prerequisite
  • 4.
    Copyright Intellipaat. Allrights reserved. Prerequisite First class function First class functions allows us to treat functions as any other objects, so that we can use them to:  pass as an argument to another function  return the function from another function  assign the function to another variable Nested function Python supports the concept of a "nested function" or "inner function", which is simply a function defined inside another function. Note: Not to be confused with recursive functions
  • 5.
    Copyright Intellipaat. Allrights reserved. Definition
  • 6.
    Copyright Intellipaat. Allrights reserved. Definition Closure is an inner function that remembers and has access to the variables in the local scope in which it was created even after the outer function has finished executing. A closure closes over their free variable from their environment
  • 7.
    Copyright Intellipaat. Allrights reserved. Example
  • 8.
    Copyright Intellipaat. Allrights reserved. Use case
  • 9.
    Copyright Intellipaat. Allrights reserved. Use case  To replace the unnecessary use of class: Suppose you have a class that contains just one method besides the __init__ method. In such cases, it is better to use a closure instead of a class.  To avoid the use of the global scope: If you have global variables which only one function in your program will use, think closure. Define the variables in the outer function and use them in the inner function.  To implement data hiding: The only way to access the enclosed function is by calling the enclosing function. There is no way to access the inner function directly.  To remember a function environment even after it completes its execution: You can then access the variables of this environment later in your program.
  • 10.
    Copyright Intellipaat. Allrights reserved. • Further Learning..
  • 11.
    Copyright Intellipaat. Allrights reserved. Further Learning
  • 12.
    Copyright Intellipaat. Allrights reserved. India: +91-7022374614 US: 1-800-216-8930 (TOLL FREE) support@intellipaat.com 24/7 Chat with Our Course Advisor

Editor's Notes