func_list= ["function1", "function2", "function3"] class doit(object): def __init__(self): for item in func_list: getattr(self, item)() def function1(self): print "f1" def function2(self): print "f2" def function3(self): print "f3" >>> doit() f1 f2 f3
For private functions:
for item in func_list: if item.startswith('__'): getattr(self, '_' + self.__class__.__name__+ item)() else: getattr(self, item)()
.
getattr(object, name[, default])
Gets the value of the named attribute of the object. name must be a string. If the string is the name of one of the attributes of the objects, the result will be the value of this attribute. For example, getattr (x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, the default value is returned, if provided, otherwise an AttributeError will be raised.
http://docs.python.org/library/functions.html#getattr
source share