I've been completely stumped on this one:
I have the following python code:
def remove(self, widgets):
for widget in widgets:
widget_found = False
for widget_sig in self.widgets:
if widget_sig.id == widget:
#remove all objects from selected widget
widget_found = True
to_remove = widget_sig.objs
for obj in to_remove:
#objs are all intances of oo_canvas classes
obj.destroy()
self._build(widget, obj)
if not widget_found:
#if we iterated through the entire list and still couldn't find anything
raise mockingbird_errs.InternalMockingbirdError("The requested widget was not registered with this builder: "+str(widget))
This should be pretty straight forward. The thing is, it never iterates through to_remove correctly. For some reason, it skips every other element. Even more baffling, is if I have it print the length of to_remove before and after the for loop, it prints 254 and 127. Huh? As far as I know, iterating through a list does not involve deleting every other element.
Am I missing something straightforward? What on earth is going on?
解决方案... it skips every other element.
That's because you keep deleting them, shortening the list by 1. And then you move on to the next index. Either work backwards, or iterate over a copy of the list.