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 a reason to keep del in Python, or is it a vestige of Python’s pre-garbage collection days?
Firstly, you can del other things besides local variables
Both of which should be clearly useful. Secondly, using
delon a local variable makes the intent clearer. Compare:to
I know in the case of
del foothat the intent is to remove the variable from scope. It’s not clear thatfoo = Noneis doing that. If somebody just assignedfoo = NoneI might think it was dead code. But I instantly know what somebody who codesdel foowas trying to do.