17

What are the best practices and recommendations for using explicit del statement in python? I understand that it is used to remove attributes or dictionary/list elements and so on, but sometimes I see it used on local variables in code like this:

def action(x):
    result = None
    something = produce_something(x)
    if something:
        qux = foo(something)
        result = bar(qux, something)
        del qux
    del something
    return result

Are there any serious reasons for writing code like this?

Edit: consider qux and something to be something "simple" without a __del__ method.

4 Answers 4

37

I don't remember when I last used del -- the need for it is rare indeed, and typically limited to such tasks as cleaning up a module's namespace after a needed import or the like.

In particular, it's not true, as another (now-deleted) answer claimed, that

Using del is the only way to make sure a object's __del__ method is called

and it's very important to understand this. To help, let's make a class with a __del__ and check when it is called:

>>> class visdel(object):
...   def __del__(self): print 'del', id(self)
... 
>>> d = visdel()
>>> a = list()
>>> a.append(d)
>>> del d
>>>

See? del doesn't "make sure" that __del__ gets called: del removes one reference, and only the removal of the last reference causes __del__ to be called. So, also:

>>> a.append(visdel())
>>> a[:]=[1, 2, 3]
del 550864
del 551184

when the last reference does go away (including in ways that don't involve del, such as a slice assignment as in this case, or other rebindings of names and other slots), then __del__ gets called -- whether del was ever involved in reducing the object's references, or not, makes absolutely no difference whatsoever.

So, unless you specifically need to clean up a namespace (typically a module's namespace, but conceivably that of a class or instance) for some specific reason, don't bother with del (it can be occasionally handy for removing an item from a container, but I've found that I'm often using the container's pop method or item or slice assignment even for that!-).

Sign up to request clarification or add additional context in comments.

4 Comments

Alex, pretty soon Python's technical writers are going to be without jobs ; ) Thanks for another great answer.
@Dana, sure hope not, since I'm such a writer myself;-). @jhwist, thanks!
One could expect that all __del__ s are called when the interpreter exits - that is not the case. By removing refs yourself you can make sure all __del__ are called, but with is a much better approach.
Yep, with is better (try/finally in old Python versions has similar power to modern with, but is less handy).
4

No.

I'm sure someone will come up with some silly reason to do this, e.g. to make sure someone doesn't accidentally use the variable after it's no longer valid. But probably whoever wrote this code was just confused. You can remove them.

1 Comment

+1: The del statement is for wizards only. My advice is the same: unless you have a problem that's actually solved by deleting an object, don't waste time on this.
3

When you are running programs handling really large amounts of data ( to my experience when the totals memory consumption of the program approaches something like 1GB) deleting some objects: del largeObject1 del largeObject2 … can give your program the necessary breathing room to function without running out of memory. This can be the easiest way to modify a given program, in case of a “MemoryError” runtime error.

Comments

1

Actually, I just came across a use for this. If you use locals() to return a dictionary of local variables (useful when parsing things) then del is useful to get rid of a temporary that you don't want to return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.