How do I execute an def in python from input
Question: For example, I have:

    
    
    def function():
        return
    

and I want to execute it via:

    
    
    d = input('Enter the def name: ')
    

by entering the def name('function' in this case).

How would I do this?

===================EDIT==========================

Found the way!

    
    
    def func():
        print('IT WORKS!')
    
    r = raw_input('Function: ')
    result = eval(r+'()')
    


Answer: Following on @jonrsharpe's comment, you can get to a function via the module's
namespace. If you are in the module with the function already, you can

    
    
    f_name = raw_input('Enter the def name: ')
    globals()[f_name]()
    

In this case, KeyError is raised if the name is not correct.

If the function is in a different module, you can

    
    
    import functions
    f_name = raw_input('Enter the def name: ')
    getattr(functions, f_name)()
    

In this case, AttributeError is raised if the name is not correct.

