title: Why is Python's re module not working in this method?
body: <p>I'm having trouble creating an instance of a class using Python's re module. Here's what I'm trying to do:</p>
  
  <ol>
  <li>Loop over each line of many data files.</li>
  <li>If a line matches the format of a record, then create an instance of the Record class with the record's two values as attributes.</li>
  </ol>
  
  <p>I expect the following code fragment to print the string of five capital letters captured by the <code>re</code> module in the <code>terminal()</code> method for the Record class, but clearly I'm misunderstanding something. The actual output follows below the code. </p>
  
  <pre><code>    class SrcFile:
         def __init__(self, which):
            self.name = which       
  
      class Record(SrcFile):       
         def terminal(self):
            recordline = re.compile(r"^([A-Z]{5})\s{3}")
            if recordline.match(self):
               m = recordline.match(self)
               return m.group(1)
  
      for f in files:
         file = SrcFile(f)
         for l in f:
            record = Record(f)
            print(record.terminal())
  </code></pre>
  
  <p>Again, I expect to see a string of five capital letters for each record line, but what I actually get is:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./next.py", line 78, in &lt;module&gt;
      print(record.terminal())
    File "./next.py", line 63, in terminal
      if recordline.match(self):
  TypeError: expected string or buffer
  </code></pre>
  
  <p>It would also be helpful if someone could explain why, in the code</p>
  
  <pre><code>for f in files:
     file = SrcFile(f)
     for l in f:
        record = Record(f)
  </code></pre>
  
  <p>it is apparently incorrect to use <code>record = Record(file)</code>. I discovered this by trial and error, as I couldn't access the methods of the SrcFile class for a file using record.method() with the incorrect code, but I don't understand why.</p>
  
  <p>I'm certain that my inexperience with programming in general and Python in particular is rather obvious. Thanks in advance for your help.</p>