title: Making a Python script Object-Oriented
body: <p>I'm writing an application in Python that is going to have a lot of different functions, so logically I thought it would be best to split up my script into different modules. Currently my script reads in a text file that contains code which has been converted into tokens and spellings. The script then reconstructs the code into a string, with blank lines where comments would have been in the original code.</p>
  
  <p>I'm having a problem making the script object-oriented though. Whatever I try I can't seem to get the program running the same way it would as if it was just a single script file. Ideally I'd like to have two script files, one that contains a class and function that cleans and reconstructs the file. The second script would simply call the function from the class in the other file on a file given as an argument from the command line. This is my current script:</p>
  
  <pre><code>import sys
  
  tokenList = open(sys.argv[1], 'r')
  cleanedInput = ''
  prevLine = 0
  
  for line in tokenList:
  
      if line.startswith('LINE:'):
          lineNo = int(line.split(':', 1)[1].strip())
          diff = lineNo - prevLine - 1
  
          if diff == 0:
              cleanedInput += '\n'
          if diff == 1:
              cleanedInput += '\n\n'
          else:
              cleanedInput += '\n' * diff
  
          prevLine = lineNo
          continue
  
      cleanedLine = line.split(':', 1)[1].strip()
      cleanedInput += cleanedLine + ' '
  
  print cleanedInput
  </code></pre>
  
  <p>After following Alex Martelli advice below, I now have the following code which gives me the same output as my original code.</p>
  
  <pre><code>def main():
      tokenList = open(sys.argv[1], 'r')
      cleanedInput = []
      prevLine = 0
  
      for line in tokenList:
  
          if line.startswith('LINE:'):
              lineNo = int(line.split(':', 1)[1].strip())
              diff = lineNo - prevLine - 1
  
              if diff == 0:
                  cleanedInput.append('\n')
              if diff == 1:
                  cleanedInput.append('\n\n')
              else:
                  cleanedInput.append('\n' * diff)
  
              prevLine = lineNo
              continue
  
          cleanedLine = line.split(':', 1)[1].strip()
          cleanedInput.append(cleanedLine + ' ')
  
      print cleanedInput
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>I would still like to split my code into multiple modules though. A 'cleaned file' in my program will have other functions performed on it so naturally a cleaned file should be a class in its own right?</p>