When (and why) to use del?
Duncan Booth
duncan.booth at invalid.invalid
Tue Dec 9 13:55:05 EST 2008
Albert Hopkins <marduk at letterboxes.org> wrote:
> def otherfunction():
> try:
> # some stuff
> except SomeException, e:
> # more stuff
> del e
> return
>
>
> I think this looks ugly, but also does it not hurt performance by
> preempting the gc? My feeling is that this is a misuse of 'del'. Am I
> wrong? Is there any advantage of doing the above?
>
It is probably a complete waste of time, but there are situations where
code similar to this can be useful:
def otherfunction():
try:
# some stuff
except SomeException, e:
# more stuff
del e
raise
return
The point of code like this is that when a function exits by throwing an
exception the traceback includes a reference to the stack frame and all the
local variables. In some situations that can result in large data
structures not being freed for a very long time (e.g. until another
exception is thrown that is handled at the same level). So, *in very
specialised situations* it may be important to delete particular names from
the local namespace.
More information about the Python-list
mailing list