calling a function indirectly

Fernando Pérez fperez528 at yahoo.com
Tue Feb 19 16:32:17 EST 2002


Rajarshi Guha wrote:

> Hi,
> is it possible to have a variable contain a function name, and use the
> variable to call the function?
> An example:
> 
> def func:
> some code
> 
> funcvar = 'func'
> 
> Now use funcvar (somehow!) to call func
> 
Just use

funcvar = func

(no quotes) and you're done. In python, 'everything is a name' (which points 
to an object). func is just a name pointing to a function object, and the 
assignment makes funcvar be a second name pointing to the same thing. Think 
of it as C pointers which do all referencing/dereferencing/memory management 
magically for you.

cheers,

f



More information about the Python-list mailing list