programmatically calling a function

Larry Bates lbates at syscononline.com
Mon Mar 7 17:01:24 EST 2005


I see lots of others have made suggestions, but here is a method
that I use frequently:

define a dictionary that contains references to your functions:

def foo():
.
. whatever it does
.

def bar():
.
. whatever it does
.



xfer={'foo', foo, 'bar', bar}

Then you can write

for fname in fnames:
    xfer[fname](args)

May not be what you want, but seems like it is from your description.

-Larry


Dave Ekhaus wrote:
> hi
> 
>     i'd like to call a python function programmatically - when all i
> have is the functions name as a string.  i.e.
> 
>     
> fnames = ['foo', 'bar']
> 
> for func in fnames:
> 
>     #
>     # how do i call function 'func' when all i have is the name of the
> function ???
>     #
>     
> 
> 
> def foo():
>     
>     print 'foo'
> 
> def bar():
> 
>     print 'bar'
> 
> 
>     i'd really appreciate any help the 'group' has to offer.
> 
> 
> thanks
> dave
> 



More information about the Python-list mailing list