Python language problem

Diez B. Roggisch deets at nospam.web.de
Wed Jun 7 13:47:06 CEST 2006


> 
> Thanks for your so detailed explain, I think I know you, But my Engish
> is not enough
> to explain.
> 
> I create same object in Tree, I want to update Tree, when I need to
> delete subtree.
> If where no references, I can't do that. some thing like blow:
> for i in list:
>    del i


This makes no sense. In python, objects are garbage collected when there are
no references to the anymore. What you do is 

 - create a reference to an object by letting i point to it

 - remove i, such that the reference is removed.

This has no effect at all --  1 - 1 = 0

If you want to remove items from a list, do something like this:

l[:] = [e for e in l if predicate(e)]

This will alter the list l such that only the elements of it that fullfill
the predicate are retained.

In other words:

your code above should work when written like this:

l[:] = []

which effectively removes all elements from the list. 

This does NOT mean that the objects referenced to by the list are deleted! 
If they are referenced from anywhere else, they are kept!!

Diez



More information about the Python-list mailing list