calling a function indirectly

Grant Edwards grante at visi.com
Wed Feb 20 05:30:15 CET 2002


In article <a4v7s6$13tk at r02n01.cac.psu.edu>, 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

How about this:

    def func():
        print "hello"
    
    x = func

    x()

It doesn't use the name of the function, but if you really want
to do it that way you can do this:

    def func():
        print "hello"
    
    x = "func"

    eval(x)()

-- 
Grant Edwards                   grante             Yow!  I want a VEGETARIAN
                                  at               BURRITO to go... with
                               visi.com            EXTRA MSG!!



More information about the Python-list mailing list