title: Removing handlers from python's logging loggers
body: <p>I am playing with Python's logging system. I have noticed a strange behavior while removing handlers from a Logger object in a loop. Namely, my for loop removes all but one handler. Additional call to <code>.removeHandler</code> removes the last handler smoothly. No error messages are issued during the calls.</p>
  
  <p>This is the test code:</p>
  
  <pre><code>import logging
  import sys
  logging.basicConfig()
  dbg = logging.getLogger('dbg')
  dbg.setLevel(logging.DEBUG)
  
  testLogger = logging.getLogger('mylogger')
  sh = logging.StreamHandler(sys.stdout)
  fh = logging.FileHandler('mylogfile.log')
  dbg.debug('before adding handlers: %d handlers'%len(testLogger.handlers))
  testLogger.addHandler(fh)
  testLogger.addHandler(sh)
  
  dbg.debug('before removing. %d handlers: %s'%(len(testLogger.handlers), 
                                                str(testLogger.handlers)))
  for h in testLogger.handlers:
      dbg.debug('removing handler %s'%str(h))
      testLogger.removeHandler(h)
      dbg.debug('%d more to go'%len(testLogger.handlers))
  
  #HERE I EXPECT THAT NO HANDLER WILL REMAIN    
  dbg.debug('after removing: %d handlers: %s'%(len(testLogger.handlers), 
                                                str(testLogger.handlers)))
  if len(testLogger.handlers) &gt; 0:
      #Why is this happening?
      testLogger.removeHandler(testLogger.handlers[0])
  dbg.debug('after manually removing the last handler: %d handlers'%len(testLogger.handlers))    
  </code></pre>
  
  <p>I expect that at the end of the loop no handlers will remain in the <code>testLogger</code> object, however
  the last call to <code>.removeHandler</code> apparently fails, as can be seen from the output below. Nevertheless
  additional call to this function removes the handler as expected. Here is the output:</p>
  
  <pre><code>DEBUG:dbg:before adding handlers: 0 handlers
  DEBUG:dbg:before removing. 2 handlers: [&lt;logging.FileHandler instance at 0x021263F0&gt;, &lt;logging.StreamHandler instance at 0x021262B0&gt;]
  DEBUG:dbg:removing handler &lt;logging.FileHandler instance at 0x021263F0&gt;
  DEBUG:dbg:1 more to go
  DEBUG:dbg:after removing: 1 handlers: [&lt;logging.StreamHandler instance at 0x021262B0&gt;]
  DEBUG:dbg:after manually removing the last handler: 0 handlers
  </code></pre>
  
  <p>More interestingly, if I replace the original loop with the following one, the loop
  works as expected and no handlers remain in the <code>testLogger</code> object at the end of the loop. 
  Here is the modified loop:</p>
  
  <pre><code>while len(testLogger.handlers) &gt; 0:
      h = testLogger.handlers[0]
      dbg.debug('removing handler %s'%str(h))
      testLogger.removeHandler(h)
      dbg.debug('%d more to go'%len(testLogger.handlers))
  </code></pre>
  
  <p>What explains this behaviour? Is this a bug or am I missing something?</p>