While studying Python, a closure came up and I didn't understand what I was saying, so I made a note so that I can look back when I forget it.
Basically, you can understand it by referring to the following. Python Tips: I want to use closures in Python [When do you use closures? ~ Let's list three usage scenarios](http://qiita.com/HirofumiYashima/items/ed17c83f26de3d510b93#python-tipspython%E3%81%A7%E3%82%AF%E3%83%AD%E3%83 % BC% E3% 82% B8% E3% 83% A3% E3% 82% 92% E4% BD% BF% E3% 81% 84% E3% 81% 9F% E3% 81% 84) [Python] Function (4)
Roughly speaking closures ** The value of the variable in the function is the value of the variable that passed when the function was declared (the function was read). ** ** That's what I understand now.
In such a case, 5 is stored in the variable "i" when the function is read, so even if 6 is entered in the variable "i" after that, the value returned will be 5.
i = 5
def f(arg=i):
print(arg)
i = 6
f()
http://docs.python.jp/3/tutorial/controlflow.html
I've lived in a world unrelated to closures, so why! 6 Yaro! There is a feeling. So, [When do you use closures? ~ Let's list three usage scenarios](http://qiita.com/HirofumiYashima/items/ed17c83f26de3d510b93#python-tipspython%E3%81%A7%E3%82%AF%E3%83%AD%E3%83 % BC% E3% 82% B8% E3% 83% A3% E3% 82% 92% E4% BD% BF% E3% 81% 84% E3% 81% 9F% E3% 81% 84) this.
def circle_area_func(pi):
"""Returns a function that finds the area of a circle"""
def circle_area(radius):
return pi * radius ** 2 #This pi is circle_area_func()The value specified in the argument of
return circle_area #Returns the function as a return value
#Pi 3.Generate a function to calculate the area when set to 14
ca1 = circle_area_func(3.14)
#Next, set the pi to 3.Generate a function when set to 141592
ca2 = circle_area_func(3.141592)
#The two functions created above have a radius=Get the operation result by giving 1 as an argument
ca1(1)
ca2(1)
#The two functions created above have a radius=Get the operation result by giving 2 as an argument
ca1(2)
ca2(2)
The parameters of each function have their own values, but I wasn't sure why this was the value stored in radius.
--3.14 (or 3.141592) for the parameter "pi" of circle_area_func (pi) --1 (or 2) for the parameter "radius" of circle_area (radius)
In the first place, I didn't seem to understand what was stored in ca1 of "ca1 = circle_area_func (3.14)".
I always thought that it was stored in ca1 in the function "circle_area_func (pi)", but it was not the function "circle_area (radius)".
def circle_area(radius):
return pi * radius ** 2 #This pi is circle_area_func()The value specified in the argument of
return circle_area
When "ca1 (1)" is executed in the subsequent processing, the function "circle_area (radius)" is called. Therefore, 1 of "ca1 (1)" is stored in the parameter "radius".
The variable "pi" in the function "circle_area (radius)" is a closure concept, and it just contains 3.14 that was entered as an argument in "ca1 = circle_area_func (3.14)".
It took a tremendous amount of time, but I'm hungry.
Recommended Posts