Python> function> Closure

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic (No. 2694 / 12833)

Closure

This is a function that is dynamically generated by another function and can both change and remember the values of variables that were created outside the function.

I've tried.

http://ideone.com/ehuo2k

def define_closure(member):
	def Kagemusha():
		return "%s is my decoy" % member
	return Kagemusha
	
aKgms = define_closure("7of9")
bKgms = define_closure("Tuvok")
cKgms = define_closure("Janeway")

whoIsKagemusha = aKgms()
print(whoIsKagemusha)

whoIsKagemusha = bKgms()
print(whoIsKagemusha)

whoIsKagemusha = cKgms()
print(whoIsKagemusha)

result


Success	time: 0 memory: 23304 signal:0
7of9 is my decoy
Tuvok is my decoy
Janeway is my decoy

You will learn how to use Closure soon.

https://en.wikipedia.org/wiki/Political_decoy

Recommended Posts

Python> function> Closure
python function ①
[Python] function
python function ②
python enumerate function
[Python] Generator function
Python> function> Inner function
Python function decorator
Function execution time (Python)
Python function argument summary
Python print function (sequel)
Python: About function arguments
Time floor function (Python)
Python
Use callback function in Python
[python] Value of function object (?)
[Python] Etymology of python function names
About python beginner's memorandum function
About the enumerate function (python)
python url setting include function
Python #function 2 for super beginners
Nonlinear function modeling in Python
Draw implicit function in python
Python higher-order function (decorator) sample
Immediate function in python (lie)
closure
[Python] What is a zip function?
[python] Callback function (pass function as argument)
Implement R's power.prop.test function in python
Function argument type definition in python
Included notation in Python function arguments
Write AWS Lambda function in Python
[Introduction to Udemy Python 3 + Application] 56. Closure
Measure function execution time in Python
[Python] Make the function a lambda function
Python #len function for super beginners
Function synthesis and application in Python
Basic Python operation 2nd: Function (argument)
How to use python zip function
[Python] Difference between function and method
[Python] Function arguments * (star) and ** (double star)
[OpenCV; Python] Summary of findcontours function
kafka python
Python3> Functions> Function Renaming Mechanism> f = fib
Create a Python function decorator with Class
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
Python service
python tips
Python basics
Python memo
ufo-> python (3)
Closure 4 language comparison (Python, JavaScript, Java, C ++)
Python comprehension
Precautions when pickling a function in python
install python