How do I reverse my for loop when I'm parsing a text file (python)
<p>I have a for loop which iterates through a text file (in this case actually a Python file) and it is trying to extract all the functions (looking for the word <code>def</code>). Once it finds that word it starts recording lines until it hits a blank space (which I'm using to denote the end of the function).</p>

<p>My problem is that I want to backup once I hit a <code>def</code> in the file and record any comments that might come before the function. Ex: <code># This function does the following...</code> etc. I want to backup until I no longer hit a hash.</p>

<p>How would I look backwards with this loop I have written?</p>

<pre><code>for (counter,line) in enumerate(visible_texts):
    line= line.encode('utf-8')
# if line doesn't contain def then ignore it
    if "def" in line and infunction== 0:
        match = re.search(r'\def (\w+)', line.strip())
        line = line.split ("def")[1]    
        print "Recording start of the function..."
        # Backup to see if there's any hashes above it (until the end of the hashes) ** how do I do this **
</code></pre>

<p>An example of output I would want at the end would be:</p>

<pre><code># This function was created by Thomas
# This function print a pass string into the function    
def printme( str ):
       "This prints a passed string into this function"
       print str
       return
</code></pre>
