[PYTHON] If you want to put an argument in the closure function and execute it later

python


def outer(a, b):

    def inner():
        return a + b

    return inner#Returns an object instead of calling the inner function

f = outer(1,3)
print(f)
print(f())

Execution result


<function outer.<locals>.inner at 0x7**c3dfa*****>
4

in print (f) The information of the inner object is returned, a + b is not running.

If you do f (), inner will be executed.

Recommended Posts

If you want to put an argument in the closure function and execute it later
If an exception occurs in the function, it will be transmitted to the caller 2
If an exception occurs in the function, it will be transmitted to the caller 1
I want to pass an argument to a python function and execute it from PHP on a web server
Understand Python yield If you put yield in a function, it will change to a generator
I want to copy an English paper from pdf and put it in Google Translate
If you just want to get the dump file of the server, it was convenient to build an http server
I want to get the file name, line number, and function name in Python 3.4
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
I want to replace the variables in the python template file and mass-produce it in another file.
It is easy to execute SQL with Python and output the result in Excel
What to do if you run python in IntelliJ and end with an error
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Read the function name from the DB and execute it dynamically
[Python] If you suddenly want to create an inquiry form
Check the argument type annotation when executing a function in Python and make an error
[Python] When you want to import and use your own package in the upper directory
An easy way to view the time taken in Python and a smarter way to improve it
[Subprocess] When you want to execute another Python program in Python code
Don't write Python if you want to speed it up with Python
If you give a list with the default argument of the function ...
When you want to save the result of the callback function somewhere
I want to create a pipfile and reflect it in docker
What to do if you can't use the trash in Lubuntu 18.04.
I want to pass an argument to a python function and execute it from PHP on a web server
The real cause of the Jubatus client spitting out a timeout and dropping an error
I made a POST script to create an issue on Github and register it in the Project
If you want to switch the execution user in the middle of a Fabric task, settings context manager
If you want a singleton in python, think of the module as a singleton
If you want to include awsebcli with CircleCI, specify the python version
Python Note: When you want to know the attributes of an object
Enclose the cat result in double quotes and put it in a variable
If you want to count words in Python, it's convenient to use Counter.
Isn't Qt the strongest library if you want to easily output SVG?
I want to leave an arbitrary command in the command history of Shell
If the conda environment is strange, let's put it back in mac
How to save the feature point information of an image in a file and use it for matching
An engineer who has noticed the emo of cryptography is trying to implement it in Python and defeat it
If you use Pandas' Plot function in Python, it is really seamless from data processing to graph creation
I want to visualize where and how many people are in the factory
If you want to display values using choices in a template in a Django model
Handle CSV that contains the element you want to parse in the file name
I want to write an element to a file with numpy and check it.
If you guys in the scope kitchen can do it with a margin ~ ♪
I made an appdo command to execute a command in the context of the app
[OpenCV] When you want to check if it is read properly with imread
Use PIL in Python to extract only the data you want from Exif
I want to store the result of% time, %% time, etc. in an object (variable)
The first step to log analysis (how to format and put log data in Pandas)
If you want to enter the virtual environment with jupyter, nb_conda_kernels is recommended
If you try to install Python2 pip after installing Python3 pip and it is rejected
If you want to create a Word Cloud.
When you want to update the chrome driver.
Fix the argument of the function used in map
An app that you must put in Linux
Make fixture an argument to parameterize in py.test
In bash, "Delete the file if it exists".
I want to use the activation function Mish
I want to display the progress in Python!
Web service (CtoC) Attract customers and monetize If you want to succeed, implement this
What to do if the print command itself causes an error in Maya python
What to do if you get an error when importing matplotlib in Python (Mac)