A memorandum of closure survey contents

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

A memorandum of closure survey contents
A small memorandum of openpyxl
A memorandum of using eigen3
[Python] A memorandum of beautiful soup4
A memorandum of files under conf.d
A memorandum of using Python's input function
A memorandum of speed of arbitrary degree diagonalization
A memorandum of understanding about django's QueryDict
A memorandum of python string deletion process
A memorandum of trouble when formatting data
Memorandum of sed
Contents of __name__
Image of closure
A memorandum of calling Python from Common Lisp
A memorandum of extraction by python bs4 request
Create a table of contents with IPython notebook
Memorandum of fastText (editing)
A memorandum about the warning of the pylint output result
Obtained contents of sosreport
A memorandum about Nan.
elasticsearch_dsl Memorandum of Understanding
[Note] Contents of shape [0], shape [1], shape [2]
[Ubuntu] How to delete the entire contents of a directory
A memorandum of stumbling on my personal HEROKU & Python (Flask)
[Introduction to AWS] A memorandum of building a web server on AWS
[Data science memorandum] Confirmation of the contents of DataFrame type [python]
A Python script that compares the contents of two directories
How to connect the contents of a list into a string
A simple sample of pivot_table.
taichi's Torisetsu ⓪ Table of contents
A memorandum about correlation [Python]
A memorandum about Python mock
A memorandum regarding γ conversion
A brief summary of Linux
Simulation of the contents of the wallet
A memorandum of understanding for the Python package management tool ez_setup
Process the contents of the file in order with a shell script
[python, ruby] fetch the contents of a web page with selenium-webdriver
A memorandum of scraping & machine learning [development technique] by Python (Chapter 5)
Create a function to get the contents of the database in Go
A memorandum regarding the acquisition of the Python3 engineer certification basic exam
[Python] A program that rotates the contents of the list to the left