How to create a function object from a string

When defining a function in Python, you would normally use the def statement and write it as follows.

python


def foo(x):
    print(x)

What this function definition statement does is create a function object and assign it to the name foo in the global namespace.

If the Python script that defines this function is given as text from the outside, the following code will achieve the same processing as the above function definition.

python


func = """def foo(x):
    print(x)
"""
exec(func, globals())

The Built-in function ʻexecexecutes the Python code (string or Code object) of the first argument in the namespace of the second argument. In this example, the second argument,globals (), passes the global namespace for this module, so a function object with the name foois created in it. That is,foo is created just as it would normally be defined in a defstatement. In fact, when you runfoo, you can see that it can be used in the same way as it is normally defined in the def` statement.

python


>>> foo("hello")
hello

Recommended Posts

How to create a function object from a string
How to create a clone from Github
How to create a repository from media
How to generate a Python object from JSON
Create a datetime object from a string in Python (Python 3.3)
How to call a function
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
Create a pandas Dataframe from a string.
How to create a Conda package
How to make a recursive function
How to create a virtual bridge
Python-dynamically call a function from a string
How to create a Dockerfile (basic)
How to create a config file
How to create a git clone folder
[Python] How to invert a character string
[Introduction to Python] How to split a character string with the split function
How to get a string from a command line argument in python
How to extract the desired character string from a line 4 commands
Edit Excel from Python to create a PivotTable
How to create a Python virtual environment (venv)
How to open a web browser from python
How to embed a variable in a python string
How to create a JSON file in Python
How to extract coefficients from a fractional formula
How to create a shortcut command for LINUX
How to create a wrapper that preserves the signature of the function to wrap
[Note] How to create a Ruby development environment
How to create a Kivy 1-line input box
How to create a multi-platform app with kivy
[Introduction to Python] How to write a character string with the format function
How to create a Rest Api in Django
[Go] How to write or call a function
How to Mock a Public function in Pytest
[Note] How to create a Mac development environment
[Python] I tried to get the type name as a string from the type function
Read the Python-Markdown source: How to create a parser
How to create an article from the command line
How to convert / restore a string with [] in python
Create a function to visualize / evaluate the clustering result
[Blender x Python] How to create an original object
How to create a submenu with the [Blender] plugin
How to convert a class object to a dictionary with SQLAlchemy
[Python] How to expand variables in a character string
[Go] How to create a custom error for Sentry
How to create a local repository for Linux OS
Consider a conversion from a Python recursive function to a non-recursive function
Execute Python function from Powershell (how to pass arguments)
How to create a simple TCP server / client script
How to post a ticket from the Shogun API
How to take a captured image from a video (OpenCV)
Python script to create a JSON file from a CSV file
[Python] How to create a 2D histogram with Matplotlib
Create a function in Python
How to hack a terminal
How to create a radial profile from astronomical images (Chandra, XMM etc.) using python
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
Let's create a function for parametrized test using frame object
Create a function to display images like Jupyter / RStudio [Docker]
How to slice a block multiple array from a multiple array in Python