'reload M' doesn't update 'from M inport *'

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 9 21:13:28 EDT 2010


On Fri, 09 Jul 2010 23:14:23 +0200, Frederic Rentsch wrote:

> I understand that things going out of scope delete themselves. I have
> used del on occasion, for instance, to get rid of invalid members of a
> list or a dictionary. It has to be done in two passes, though, because
> neither can be altered during an iteration. The first pass makes a
> delete list of indices or keys, so that the actual deletion iterates
> through the delete list, not the object deleted from.
>    Would you call that a misuse?

No, that is perfectly fine. Pay no attention to anyone who says that del 
should be avoided. Like all tools, it can be used badly or inefficiently, 
or by somebody who doesn't understand it, but that's no reason to avoid 
using del correctly.

Another strategy is to make a copy of the dict/list and iterate over 
that. Copies are fast, since you're only copying the top-level list or 
dict and not the underlying objects within it.

A third is to forget about deleting and create a new list containing only 
the objects you don't want to delete. All three strategies (two using 
del, one without) have different use-cases and are perfectly acceptable.



-- 
Steven



More information about the Python-list mailing list