loop through all methods in a python program
Question: I want to do something like this:

    
    
    def a():
      # do stuff
      return stuff
    
    def b():
      # do stuff
      return different_stuff
    
    def c():
      # do one last thing
      return 200
    
    for func in this_file:
      print func_name
      print func_return_value
    

I essentially want to mimic this flask app, without the flask parts:

    
    
    app = Flask(__name__)
    app.register_blueprint(my_bp, url_prefix='/test')
    my_bp.data = fake_data
    
    def tests():
      with app.test_client() as c:
        for rule in app.url_map.iter_rules():
          if len(rule.arguments) == 0 and 'GET' in rule.methods:
            resp = c.get(rule.rule)
            log.debug(resp)
            log.debug(resp.data)
    

is this possible?


Answer: Like this:

    
    
    import sys
    
    # some functions...
    def a():
       return 'a'
    
    def b():
       return 'b'
    
    def c():
       return 'c'
    
    # get the module object for this file    
    module = sys.modules[__name__]
    
    # get a list of the names that are in this module  
    for name in dir(module):
       # get the Python object from this module with the given name
       obj = getattr(module, name)
       # ...and if it's callable (a function), call it.
       if callable(obj):
          print obj()
    

running this gives:

    
    
    bgporter@varese ~/temp:python moduleTest.py
    a
    b
    c
    

Note that the functions will not necessarily be called in the order of
definition as they are here.

