I can't really think of any reason why python needs the del
keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign None
to it. And when deleting from a dictionary, a del
method could be added.
Is there any reason to keep del
in python, or is it a vestige of Python's pre-garbage collection days?
I think one of the reasons that del has its own syntax is that replacing it with a function might be hard in certain cases given it operates on the binding or variable and not the value it references. Thus if a function version of del were to be created a context would need to be passed in. del foo would need to become globals().remove('foo') or locals().remove('foo') which gets messy and less readable. Still I say getting rid of del would be good given its seemingly rare use. But removing language features/flaws can be painful. Maybe python 4 will remove it :)
Using "del" explicitly is also better practice than assigning a variable to None. If you attempt to del a variable that doesn't exist, you'll get a runtime error but if you attempt to set a variable that doesn't exist to None, Python will silently set a new variable to None, leaving the variable you wanted deleted where it was. So del will help you catch your mistakes earlier
There is a specific example of when you should use
del
(there may be others, but I know about this one off hand) when you are usingsys.exc_info()
to inspect an exception. This function returns a tuple, the type of exception that was raised, the message, and a traceback.The first two values are usually sufficient to diagnose an error and act on it, but the third contains the entire call stack between where the exception was raised and where the the exception is caught. In particular, if you do something like
the traceback,
tb
ends up in the locals of the call stack, creating a circular reference that cannot be garbage collected. Thus, it is important to do:to break the circular reference. In many cases where you would want to call
sys.exc_info()
, like with metaclass magic, the traceback is useful, so you have to make sure that you clean it up before you can possibly leave the exception handler. If you don't need the traceback, you should delete it immediately, or just do:To avoid it all together.
Firstly, you can del other things besides local variables
Both of which should be clearly useful. Secondly, using
del
on a local variable makes the intent clearer. Compare:to
I know in the case of
del foo
that the intent is to remove the variable from scope. It's not clear thatfoo = None
is doing that. If somebody just assignedfoo = None
I might think it was dead code. But I instantly know what somebody who codesdel foo
was trying to do.Every object in python has an identifier, Type, reference count associated with it, when we use del the reference count is reduced, when the reference count becomes zero it is a potential candidate for getting garbage collected. This differentiates the del when compared to setting an identifier to None. In later case it simply means the object is just left out wild( until we are out of scope in which case the count is reduced) and simply now the identifier point to some other object(memory location).
del
is often seen in__init__.py
files. Any global variable that is defined in an__init__.py
file is automatically "exported" (it will be included in afrom module import *
). One way to avoid this is to define__all__
, but this can get messy and not everyone uses it.For example, if you had code in
__init__.py
likeThen your module would export the
sys
name. You should instead write