** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
It will be difficult content, so at this stage it is OK if you understand that there is something like this.
closure
def circle_area_func(pi):
#Passing radius returns the area of the circle
def circle_area(radius):
return pi * radius * radius
# circle_Returns area but does not execute
return circle_area
#circle with pi as 3_area_Passed to func and returned circle_Substitute area for cal1
cal1 = circle_area_func(3)
#pi 3.14 as circle_area_Passed to func and returned circle_Substitute area for cal2
cal2 = circle_area_func(3.14)
#Pass radius to cal1 with radius 10 and print the result
print(cal1(10))
#Pass radius to cal2 as 10 and print the result
print(cal2(10))
result
300
314.0
The closure is like this time
When you want to use the argument (pi) set at the beginning according to the purpose later (whether to set it to 3 or 3.14)
It's an effective method, so keep it in the corner of your head.
Recommended Posts