How to call Python functions dynamically
Question: I have this code:

    
    
    fields = ['name','email']
    
    def clean_name():
        pass
    
    def clean_email():
        pass
    

How can I call `clean_name()` and `clean_email()` dynamically?

For example:

    
    
    for field in fields:
        clean_{field}()
    

I used the curly brackets because it's how I used to do it in PHP but
obviously doesn't work.

How to do this with Python?


Answer: If don't want to use `globals, vars` and don't want make a separate module
and/or class to encapsulate functions you want to call dynamically, you can
call them as the attributes of the current module:

    
    
    import sys
    ...
    getattr(sys.modules[__name__], "clean_%s" % fieldname)()
    

