Skip to content Skip to sidebar Skip to footer

Python 2.7.2 Doesn't Properly Iterate Through Logger's Handlers

the following code is very simple, but for some reason, for-loop does not iterate through all handlers of the logger. however, for-loop does iterate through all handlers if we remo

Solution 1:

This is a classic destructive iteration. What you are doing is:

>>>l= [0, 1, 2, 3, 4]>>>for n in l:...    l.remove(n)...>>>l
[1, 3]

In this example only every second element is removed. Why? Well the for...in syntax obscures what's actually happening, which is the same as in a traditional C-style for-index loop:

>>>l= [0, 1, 2, 3, 4]>>>i= 0>>>while i<len(l):...del l[i]...    i+= 1...>>>l
[1, 3]

So first time round the loop i is 0. Item 0 gets removed, moving items 1–4 down one place to be the new items 0–3. Next time round the loop i is 1 so the current item 1, which is 2, is removed. The original 1 has been jumped and remains in the loop.

A simple workaround to avoid destructive iteration is to take a copy of the list and iterate the copy whilst changing the original:

for handler in list(my_logger.handlers):

Using filter or list comprehensions is typically a simpler route when you are trying to remove certain items from a list.

Post a Comment for "Python 2.7.2 Doesn't Properly Iterate Through Logger's Handlers"