What does "del" actually do?

J

John Nagle

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.

John Nagle
 
C

Calvin Spealman

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 objec to, but thats a seperate 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.
 
S

Steve Holden

Calvin Spealman wrote [top-posting, which I have corrected]:
> 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
 
B

Bruno Desthuilliers

John Nagle a écrit :
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?

It would have been simpler to just test:.... def __init__(self, name):
.... self.name = name
.... say = "whoo"
.... def greetings(self):
.... print "%s from %s %s" \
.... %(self.say, self.__class__.__name__, self.name)
....
the del statement remove a name from the current namespace. period. It's
also used for removing keys from dicts.
Is behavior
the same for both old and new classes?

Should it be different ?
I'm trying to break cycles to fix some memory usage problems.

GC and/or Weakrefs should do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top