title: Python dynamic function creation with custom names
body: <p>Apologies if this question has already been raised and answered.
  What I need to do is very simple in concept, but unfortunately I have not been able to find an answer for it online.</p>
  
  <p>I need to create dynamic functions in Python (Python2.7) with custom names at runtime. The body of each function also needs to be constructed at runtime but it is (almost) the same for all functions.</p>
  
  <p>I start off with a list of names.</p>
  
  <pre><code>func_names = ["func1", "func2", "func3"]
  </code></pre>
  
  <p>Note that the func_name list can hold a list of arbitrary names, so the names will NOT simply be func1, func2, func3, ....</p>
  
  <p>I want the outcome to be :</p>
  
  <pre><code>    def func1(*args):
          ...
  
      def func2(*args):
          ...
  
      def func3(*args):
          ...
  </code></pre>
  
  <p>The reason I need to do this is that each function name corresponds to a test case which is then called from the outside world.</p>
  
  <p><strong>update</strong>: There is no user input. I'm tying two ends of a much bigger module. One end determines what the test cases are and among other things, populates a list of the test cases' names. The other end is the functions themselves, which must have 1:1 mapping with the name of the test case. So I have the name of the test cases, I know what I want to do with each test case, I just need to create the functions that have the name of the test cases. Since the name of the test cases are determined at runtime, the function creation based on those test cases must be at runtime as well.</p>
  
  <p><strong>update</strong>: I can also wrap this custom named functions in a class if that would make things easier.</p>
  
  <p>I can hard-code the content of the functions (since they are almost the same) in a string, or I can base it off of a base class previously defined. Just need to know how to populate the functions with this content.</p>
  
  <p>For example:</p>
  
  <pre><code>    func_content = """
                     for arg in args:
                         print arg
                     """
  </code></pre>
  
  <p>Thanks in advance,</p>
  
  <p>Mahdi</p>