title: Python file object allows you to close a file that is already closed
body: <p>Creating code in 2.7 (for a Python class I am taking) and cross-testing it in 3.x (for me).  This code is not required.  I decided to build objects that might be useful for the future instead of simply answering the original question directly.</p>
  
  <p>Since 3.x has no file object to inherit, my object uses file instances within it  instead of inheriting from it directly.  During testing of the code which follows, I encountered an oddity.  I can close a file and then close it again and this does not trigger any kind of error.  To sanity checking this code is being built right, two questions about this:
  * why can I close the same file twice
  * the way I have written this, when I close the file, am I really closing it and freeing up the memory the file object is taking up?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>class FileData(object):
      def __init__(self, xfilename, xmode):
          self.filename = xfilename
          self.mode = xmode
          self.f = open(xfilename, 'r')
  
      def getAllFileData(self):
          self.lines = f.readlines()
          self.f.close()
  
      def getLineFromFile(self):
          if f.closed:
              self.f = open(xfilename, 'r')    
          self.f.readline()
  
      def fileHead(self, numRows):
          if self.f.closed:
              self.f = open(xfilename, 'r')
  
          for i in range(numRows):
              print(self.f.readline())
          self.f.close()
  </code></pre>
  
  <p>Then I ran these test lines and accidentally re-ran the line to close the file multiple times.  To make sure I wasn't missing something, I later organized these lines in a Jupyter cell and ran them together.  </p>
  
  <pre><code>chatLog = FileData("script/record.txt", "r")
  chatLog.fileHead(15)
  
  chatLog.f.close()
  chatLog.f.close()
  </code></pre>
  
  <p>Note that fileHead() also closes the file when done, so really above code should have tried to close the same file 3 times. </p>
  
  <p>Why no error?  And is this closing the file the way I think it is?  Learning the language so any input would be appreciated.</p>