What does "del" actually do?

Steve Holden steve at holdenweb.com
Sat Feb 10 13:30:08 EST 2007


Calvin Spealman wrote [top-posting, which I have corrected]:
> On 2/10/07, John Nagle <nagle at animats.com> wrote:
>>     The Python "reference manual" says, for "del", "Rather that spelling it out
>> in full details, here are some hints."  That's not too helpful.
>>
>>     In particular, when "del" is applied to a class object, what happens?
>> Are all the instance attributes deleted from the object?  Is behavior
>> the same for both old and new classes?
>>
>>     I'm trying to break cycles to fix some memory usage problems.
>>
 > del simply removes the name in the current scope. if that happens to
 > be the last non-cyclic reference to the object it was bound to, then
 > it will remove the object to, but thats a separate matter. if you
 > remove the class and there are instances out there, they can only
 > exist if there are some other references to them, so no, they arent
 > deleted.
 >
 > del wont just delete a bunch of objects and leave broken names. i has
 > nothing to do with deleting objects, only names.
 >
Except, of course, when you aren't deleting names but elements of some 
container object.

The del statement removes references to objects as well as removing 
names from namespaces and elements from container objects. Calvin is 
correct, though. Since each instance of a class contains a reference to 
the class, the class will live on even after it is deleted by name, 
being garbage-collected only after all instances have ceased to exist.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Blog of Note:          http://holdenweb.blogspot.com
See you at PyCon?         http://us.pycon.org/TX2007




More information about the Python-list mailing list