If you're calling a method on an object (including imported modules) you can use:
getattr(obj, method_name)(*args) # for this question: use t[i], not method_name
for example:
>>> s = 'hello'
>>> getattr(s, 'replace')('l', 'y')
'heyyo'
If you need to call a function in the current module
getattr(sys.modules[__name__], method_name)(*args)
where args
is a list or tuple of arguments to send, or you can just list them out in the call as you would any other function. Since you are in a method trying to call another method on that same object, use the first one with self
in place of obj
getattr takes an object and a string, and does an attribute lookup in the object, returning the attribute if it exists. obj.x
and getattr(obj, 'x')
achieve the same result. There are also the setattr
, hasattr
, and delattr
functions if you want to look further into this kind of reflection.
A completely alternative approach:
After noticing the amount of attention this answer has received, I am going to suggest a different approach to what you're doing. I'll assume some methods exist
def methA(*args): print 'hello from methA'
def methB(*args): print 'bonjour de methB'
def methC(*args): print 'hola de methC'
To make each method correspond to a number (selection) I build a dictionary mapping numbers to the methods themselves
id_to_method = {
0: methA,
1: methB,
2: methC,
}
Given this, id_to_method[0]()
would invoke methA
. It's two parts, first is id_to_method[0]
which gets the function object from the dictionary, then the ()
calls it. I could also pass argument id_to_method[0]("whatever", "args", "I", "want)
In your real code, given the above you would probably have something like
choice = int(raw_input('Please make a selection'))
id_to_method[choice](arg1, arg2, arg3) # or maybe no arguments, whatever you want