Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function pointer

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?

like image 629
schneck Avatar asked Feb 17 '10 18:02

schneck


People also ask

What is function pointer in Python?

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.

What is function pointer?

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.

Can you do pointers in Python?

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.


1 Answers

funcdict = {   'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction,     .... }  funcdict[myvar](parameter1, parameter2) 
like image 169
Ignacio Vazquez-Abrams Avatar answered Sep 27 '22 22:09

Ignacio Vazquez-Abrams