469,365 Members | 2,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,365 developers. It's quick & easy.

What does "del" actually do?

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
Feb 10 '07 #1
3 2394
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.

On 2/10/07, John Nagle <na***@animats.comwrote:
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
--
http://mail.python.org/mailman/listinfo/python-list

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/
Feb 10 '07 #2
Calvin Spealman wrote [top-posting, which I have corrected]:
On 2/10/07, John Nagle <na***@animats.comwrote:
> 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

Feb 10 '07 #3
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:
>>class Ghost(object):
.... def __init__(self, name):
.... self.name = name
.... say = "whoo"
.... def greetings(self):
.... print "%s from %s %s" \
.... %(self.say, self.__class__.__name__, self.name)
....
>>g1 = Ghost("Albert")
g2 = Ghost("Ivan")
g1.greetings()
whoo from Ghost Albert
>>del Ghost
g1.greetings()
whoo from Ghost Albert
>>g1.__class__
<class '__main__.Ghost'>
>>>
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.
Feb 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.