Python> function> Example of taking function as an argument> map (lambda x: 2 ** x, [1, 2, 3]) / locals () ['myprint'] (3.1415, 2.718)

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

There is an example of passing function as an argument.

I've tried.

http://ideone.com/KHr1xa

def myfunc(fnc, arg1, arg2):
	fnc(arg1, arg2)
	
def myprint(arg1, arg2):
    print('%s,%s' % (arg1, arg2))
    
myfunc(myprint, 3.1415, 2.718)

result


Success	time: 0 memory: 23304 signal:0
3.1415,2.718

Does the standard document seem to be related to this? Interpretation of arguments and construction of values

Matters taught

@ shiracamus's Comment taught me how to execute a function using map ().

Also, in this Comment, I learned how to call the function name as a key from the local variable dictionary and execute it.

Thank you for the information.

Recommended Posts

Python> function> Example of taking function as an argument> map (lambda x: 2 ** x, [1, 2, 3]) / locals () ['myprint'] (3.1415, 2.718)
Example of taking Python> function> * args as arguments
Utilization of lambda (when passing a function as an argument of another function)
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
[python] Callback function (pass function as argument)
Understand the function of convolution using image processing as an example
Format when passing a long string as an argument of python
Machine learning of sports-Analysis of J-League as an example-②
Fix the argument of the function used in map
Don't take an instance of a Python exception class directly as an argument to the exception class!
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Support for Python 2.7 runtime on AWS Lambda (as of 2020.1)
Save screenshot of [Python] [Windows] screen as an image
Usage of Python locals ()
Python function argument summary
Example of using lambda
Check the scope of local variables with the Python locals function.
[Python3] Call by dynamically specifying the keyword argument of the function
Decrypt one line of code in Python lambda, map, list
Data cleansing 1 Convenient Python notation such as lambda and map