dynamic construction of variables / function names

Sakcee sakcee at gmail.com
Thu Mar 30 01:44:24 EST 2006


python provides a great way of dynamically creating fuctions calls and
class names from string

a function/class name can be stored as string and called/initilzed

e.g

def foo(a,b):
    return a+b

def blah(c,d):
    return c*d


list = ["foo", "blah"]

for func in list:
	print func(2,4)

or similar items


what is the way if the names of functions are some modification e.g

def Newfoo(a,b):
    return a+b

def Newblah(c,d):
    return c*d

list = ["foo", "blah"]

for func in list:
	print "New"+func(2,4)

or define a variable

"New"+list[0] = "First Funciton"


I think ,       print "New"+func(2,4)  and "New"+list[0] = "First
Funciton"

will not work,  either eval or exec should be used
is it correct way, is there a simple way, is this techniqe has a name?

thanks




More information about the Python-list mailing list