Here I'm suggesting you 3 methods to achieve what you want, from the easiest but most dirty (#1), to the most elegant one (#3). It's up to you to choose what's best fitted for you use case.
Resolving your methods within the scope (1/3)
well, depending on the context which you'll execute the code, you can use locals() or globals():
>>> def foo():
... print("XXX")
...
>>> locals()['foo']
<function foo at 0x266e9b0>
>>> locals()['foo']()
XXX
or
>>> globals()['foo']
<function foo at 0x266e9b0>
>>> globals()['foo']()
XXX
Because I'm at the interpreter's level. Those should work directly within a module's file.
So then, your code would become:
testData = (testSet1, testSet2, testSet3,...,testSetN)`
methods = ("method1", "method2", "method3",...,"methodN")
for data in testData:
for method in methods:
startTime = time.time()
globals()[method](data)
endTime = time.time()
Resolving your methods within a class (2/3)
A rather better way — at least IMHO — to do it would be to have all your method within a class, and thus you actually CAN use getattr():
>>> class Foo:
... def bar(self):
... print("FOOBAR!")
...
>>> f = Foo()
>>> getattr(f, 'bar')
<bound method Foo.bar of <__main__.Foo instance at 0x266d908>>
>>> getattr(f, 'bar')()
FOOBAR!
but I can understand that you would not want to create an instance for a class that is just there to keep together a bunch of methods, so you could instead use class methods:
>>> class Bar:
... @classmethod
... def foo(self):
... print("BARFOO!")
...
>>> getattr(Bar, 'foo')
<bound method classobj.foo of <class __main__.Bar at 0x7f98619e9940>>
>>> getattr(Bar, 'foo')()
BARFOO!
So your could would become:
class MyAlgorithms:
@classmethod
def method1():
pass
@classmethod
def method2():
pass
…
testData = (testSet1, testSet2, testSet3,...,testSetN)`
methods = ("method1", "method2", "method3",...,"methodN")
for data in testData:
for method in methods:
startTime = time.time()
if method in dir(MyAlgorithms):
getattr(MyAlgorithms, method)(data)
endTime = time.time()
Resolving your methods using a dict, registering them with a decorator (3/3)
And finally, my favourite way to do such a thing, is to use a dict like @RafaelCardoso suggests, but with a bit of salt and pepper:
>>> registered_methods = {}
>>> def register_method(fun):
... registred_methods[fun.__name__] = fun
... return fun
...
>>> @register_method
... def foo():
... print("XXX")
...
>>> registered_methods
{'foo': <function foo at 0x7faf37b23320>}
>>> registered_methods['foo']()
XXX
The idea is to create a global dict named methods that will contain the mapping from string to actual method, then you create a decorator called register_method, and finally you just have to decorate the method when you write it down, and it'll be added to the global methods dict!
So your could would be:
testData = (testSet1, testSet2, testSet3,...,testSetN)
methods = ("method1", "method2", "method3",...,"methodN")
for data in testData:
for method in methods:
if method in registered_methods:
startTime = time.time()
registered_methods[method](data)
endTime = time.time()
N.B.: in my examples, I considered that methods is the list of methods you want to test, not the list of methods you actually have.
HTH
getattrif you want.