dynamically generating function assignent

Bjorn Pettersen bjorn at roguewave.com
Mon Nov 1 13:59:14 EST 1999


You can do:

	fn = eval('func_' + 'a') # look it up in the environment
	fn()

-- bjorn

> -----Original Message-----
> From: anantk at my-deja.com [mailto:anantk at my-deja.com]
> Sent: Monday, November 01, 1999 11:19 AM
> To: python-list at python.org
> Subject: dynamically generating function assignent
> 
> 
> Hi,
> 
>   Here is my problem. I have defined say 12 pairs of functions
> 
> func_a()
> parse_func_a()
> 
> func_b()
> parse_func_b()
>  .
> .
> func_l()
> parse_func_l()
> 
>   A file contains a series of strings which are the names of these
> functions eg:
> 
> a
> c
> f
> b
> 
>   Now for each of these strings I need to call the two functions
> func_<x>(), and parse_func_<x>(), where <x> is the name of 
> the function
> that string refers to.
> 
>   I know you can do something like
>   call = func_a, and call effectively gets assigned to func_a. My
> question is how can I generate the RHS of this assignment dynamically?
> So that all I have to do is
> 
>   <variable> = <next line in file>
>   call1 = func_<variable>
>   call2 = parse_func_<variable>
>   call1()
>   call2()
> 
>   Any help would be appriciated.
> 
>   Thanks
> 
> -- Anant
>    anantk at yahoo.com
> 
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list