Open In App

Defining a Python function at runtime

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Python,we can define a python function at runtime execute with the help of FunctionType(). First we import types module then perform compile() function and pass parameter exec and after that with the help FunctionType() define the function at runtime.

Example 1: Function to print GEEKSFORGEEKS.

Python3




# importing the module
from types import FunctionType
  
# functttion during run-time
f_code = compile('def gfg(): return "GEEKSFORGEEKS"', "<string>", "exec")
f_func = FunctionType(f_code.co_consts[0], globals(), "gfg")
  
# calling the function
print(f_func())


Output:

GEEKSFORGEEKS

Example 2: Function to add 2 numbers.
 

Python3




# importing the module
from types import FunctionType
  
# function at run-time
f_code = compile('def gfg(a, b): return(a + b) ', "<int>", "exec")
f_func = FunctionType(f_code.co_consts[0], globals(), "gfg")
  
val1 = 3999
val2 =4999
  
# calliong the function
print(f_func(val1, val2))


Output: 

8998

Previous Article
Next Article

Similar Reads

Defining Clean Up Actions in Python
Think of a task you will always want your program to do, whether it runs perfectly or raise any kind of error. For example, We use of try statement which has an optional clause - "finally" to perform clean up actions, that must be executed under all conditions. Cleanup actions: Before leaving the try statement, "finally" clause is always executed,
3 min read
Defining DataFrame Schema with StructField and StructType
In this article, we will learn how to define DataFrame Schema with StructField and StructType. The StructType and StructFields are used to define a schema or its part for the Dataframe. This defines the name, datatype, and nullable flag for each column.StructType object is the collection of StructFields objects. It is a Built-in datatype that conta
7 min read
Python | Prompt for Password at Runtime and Termination with Error Message
Say our Script requires a password, but since the script is meant for interactive use, it is likely to prompt the user for a password rather than hardcode it into the script. Python’s getpass module precisely does what it is needed. It will allow the user to very easily prompt for a password without having the keyed-in password displayed on the use
3 min read
Architecture of Common Language Runtime (CLR)
The Common Language Runtime in the .NET Framework is the Virtual Machine component that handles program execution for various languages such as C#, F#, Visual Basic .NET, etc. The managed execution environment is provided by giving various services such as memory management, security handling, exception handling, garbage collection, thread manageme
3 min read
Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples. What is Calling a Function
4 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Returning a function from a function - Python
Functions in Python are first-class objects. First-class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. Properties of first-class functions: A function is an instance of the Object type.You can store the function in a variable.You can pass the functi
3 min read
Accessing Python Function Variable Outside the Function
In Python, variables defined within a function have local scope by default. But to Access function variables outside the function usually requires the use of the global keyword, but can we do it without using Global. In this article, we will see how to access a function variable outside the function without using "Global". Accessing Function Variab
3 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
wxPython - GetField() function function in wx.StatusBar
In this article we are going to learn about GetField() function associated to the wx.GetField() class of wxPython. GetField() function Returns the wx.StatusBarPane representing the n-th field. Only one parameter is required, that is, field number in status bar. Syntax: wx.StatusBar.GetField(self, n) Parameters: Parameter Input Type Description n in
1 min read
Practice Tags :
three90RightbarBannerImg