I have a function name stored in a variable like this:
myvar = 'mypackage.mymodule.myfunction'
and I now want to call myfunction like this
myvar(parameter1, parameter2)
What's the easiest way to achieve this?
In python, everything is object, everything means everything. If we create a variable of any type like int, float, str etc, that is also an object. So, functions are also objects in python. As we know that every object has its own id, so we can access and store that id to a variable.
A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions.
No, we don't have any kind of Pointer in Python language. The objects are passed to function by reference. The mechanism used in Python is exactly like passing pointers by the value in C.
funcdict = { 'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction, .... } funcdict[myvar](parameter1, parameter2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With