How to invoke an function on an object dynamically by name?

This question already has an answer here:

  • Calling a function of a module by using its name (a string) 10 answers

  • 使用“getattr”:http://docs.python.org/library/functions.html?highlight=getattr#getattr

    obj = MyClass()
    try:
        func = getattr(obj, "dostuff")
        func()
    except AttributeError:
        print "dostuff not found"
    
    链接地址: http://www.djcxy.com/p/55162.html

    上一篇: 我如何使用用户输入来调用Python中的函数?

    下一篇: 如何根据名称动态调用对象的函数?