Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have a str object for example: menu='install'. I want to run install method from this string. For example when I call menu(some, arguments) it will call install(some, arguments). Is there any way to do that ?

share|improve this question

marked as duplicate by toniedzwiedz, oefe, Matt S, sed, Björn Pollex Oct 11 '13 at 20:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 21 down vote accepted

If it's in a class, you can use getattr:

class MyClass(object):
    def install(self):
          print "In install"

method_name = 'install' # set by the command line options
my_cls = MyClass()
method = getattr(my_cls, method_name)
if not method:
    raise Exception("Method %s not implemented" % method_name)
method()

or if it's a function:

def install():
       print "In install"

method_name = 'install' # set by the command line options
possibles = globals().copy()
possibles.update(locals())
method = possibles.get(method_name)()    
if not method:
     raise Exception("Method %s not implemented" % method_name)
method()
share|improve this answer
1  
Thank you for your answer. But what if the method is not in a class? –  İlker Dağlı Oct 29 '11 at 2:23
    
Thank you so much sdolan. I have tried with globals not locals and it works. –  İlker Dağlı Oct 29 '11 at 2:31
    
Yup, I updated the function section to take care of this. –  sdolan Oct 29 '11 at 2:34
    
Nice! The latter method is amazing! I wouldn't have thought it possible. –  Anti Earth Oct 29 '11 at 3:03
4  
The latter does not .copy() globals before mutating it, inviting all sorts of trouble. And it has a bug, since it calls the method immediately before checking it, then calls its result again. Also, it's common practise to use a prefix, to prevent calling just ANY element in the namespace (e.g. "do_install()"). –  pyroscope Oct 29 '11 at 5:18

You can use a dictionary too.

def install():
    print "In install"

methods = {'install': install}

method_name = 'install' # set by the command line options
if method_name in methods:
    methods[method_name]() # + argument list of course
else:
    raise Exception("Method %s not implemented" % method_name)
share|improve this answer
3  
I believe using dictionary is a bit more clean that relying on globals().copy() in accepted answer. –  Victor Farazdagi Jan 22 '13 at 2:09
    
This does not work. It throws an error saying 'str' object is not callable. –  Agniva De Sarker Apr 16 '13 at 5:20
1  
@AgnivaDeSarker Make sure that in setting up your dictionary, you haven't called the function - i.e., that you use only the function name, with no brackets: {'install': install} –  Hannele Apr 25 '13 at 14:31
    
yes, my bad. I enclosed it in quotes. –  Agniva De Sarker Apr 26 '13 at 9:10

Why cant we just use eval()?

def install():
    print "In install"

New method

def installWithOptions(var1, var2):
    print "In install with options " + var1 + " " + var2

And then you call the method as below

method_name1 = 'install'
method_name2 = 'installWithOptions("a","b")'
eval(method_name1)
eval(method_name2)

This gives the output as

In install
In install with options a b
share|improve this answer
    
He is asking for a way to call the function with arguments. Can you detail? –  Mikaël Mayer Apr 25 '13 at 13:22
    
Yup it works with arguments as well in the same manner. –  Husain Khambaty Apr 25 '13 at 14:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.