Python Closures
Table Of Contents:
- What Is A Closure Method In Python?
- How To Use Closure Method In Python?
- Where To Use Closure Method In Python?
- Examples Of Closure Method.
(1) What Is A Closure Method In Python?
- As the name suggests, the method which is inside another method is called the closure method.
- Because it is closed inside the parent method, hence the name closure method.
- What the extra Python is providing here is that you can return the closure method from the parent method.
- An important thing is that the closure method will be able to remember the parent class variables, which are non-local to the closure methods.
![](https://www.praudyog.com/wp-content/uploads/2022/12/111-300x197-1.png)
(2) How To Use Closure Method In Python?
- Let us understand this by using an example.
Example:
def parent():
x = 10
y = 20
def add():
print('Addition:', x + y)
return add
obj = parent()
obj()
Output:
Addition: 30
Note:
- Here the add( ) method is the closure method because it is inside the parent( ) method.
- The first point about the closure method is that we can return it from the parent method, which we have done in ‘return add’.
- Here ‘obj’ variable is storing the closure method ‘add’.
- We can use the ‘obj’, like a function to access the add( ) method.
- The second thing about the closure method is that it will be able to remember the parent class variables, here it is ‘x’ and ‘y’.
- Hence it is printing 10 +20 = 30.
(3) Where To Use Closure Method In Python?
- When there are few methods (one method in most cases) to be implemented in a class, closures can provide an alternate and more elegant solution.
- But when the number of attributes and methods gets larger, it’s better to implement a class.
- Closures can avoid the use of global values and provides some form of data hiding.
- It can also provide an object–oriented solution to the problem.
Example:
def Student(name,rollno,subject,mark):
def details():
print('Student Name:',name)
print('Student Roll No:',rollno)
print('Student Subject:',subject)
print('Student Mark:',mark)
return details
obj = Student('Abhispa',999,'Math',98)
obj()
obj = Student('Abhispa',999,'Math',98)
obj()
Note:
- Here Student method is having the ‘details’ closure method, which is returned from the ‘Student’ method.
- You can use the ‘obj’ to call the ‘details’ method.
(4) Example Of Closure Method In Python?
Example:
def operations(x,y,operation):
def addition():
print('Addition:',x + y)
def substraction():
print('Substraction:',x-y)
def multiplication():
print('Multiplication:',x*y)
def division():
print('Division:',x/y)
if operation == 'add':
return addition
if operation == 'substract':
return substraction
if operation == 'multiply':
return multiplication
if operation == 'divide':
return division
operation1 = operations(3,4,'add')
operation2 = operations(3,4,'substract')
operation3 = operations(3,4,'multiply')
operation4 = operations(3,4,'divide')
operation1()
operation2()
operation3()
operation4()
Output:
Addition: 7
Substraction: -1
Multiplication: 12
Division: 0.75
Note:
- Here ‘operation()’ is the parent method and the method insides are the closure methods.
- You can call these closure methods to do specific operations.