title: extracting the data of a specified function in a python file | adding comments to highlight what is removed
body: <p>I want to extract the code written under a specified function. I am trying to do it like this:</p>
  
  <p>With an example file <code>TestFile.py</code> containing the following function <code>sub()</code>:</p>
  
  <pre class="lang-py prettyprint-override"><code>def sub(self,num1,num2):
      # Subtract two numbers
      answer = num1 - num2
      # Print the answer
      print('Difference = ',answer)
  </code></pre>
  
  <p>If I run <code>get_func_data.py</code>:</p>
  
  <pre class="lang-py prettyprint-override"><code>def giveFunctionData(data, function):
      dataRequired = []
      for i in range(0, len(data)):
          if data[i].__contains__(str(function)):
              startIndex = i
              for p in range(startIndex + 1, len(data)):
                  dataRequired.append(data[p])
                  if data[p].startswith('\n' + 'def'):
                      dataRequired.remove(dataRequired[len(dataRequired) - 1])
                      break
      print(dataRequired)
      return dataRequired
  
  data = []
  f = open("TestFile.py", "r")
  for everyLine in f:
      if not(everyLine.startswith('#') or everyLine.startswith('\n' + '#')):
          data.append(everyLine)
  
  giveFunctionData(data,'sub') # Extract content in sub() function
  </code></pre>
  
  <p>I expect to obtain the following result:</p>
  
  <pre><code>answer = num1 - num2
  print('Difference = ',answer)
  </code></pre>
  
  <p>But here I get the comments written inside the function as well. Instead of the list, Is there a way to get it as it is written in the file?</p>