7

The del statement has many practical uses to remove attributes and such (see When is del useful in python? and others), but it can also be used to unbind local variables:

 def test():
    x = ...
    del x

Are there any practical use-cases for this?

Garbage collection comes to mind, but x=None should have the same effect. I've also heard "clean up after import" as a reason, but from ... import ... does that better. "Cleaner code" has also popped up, but I don't see how del could make code cleaner (at least for code that isn't in desperate need of a refactoring anyway).

Edit:

This got a bit out of hand. I was expecting an answer such as

"Of course you need del x! You couldn't do ... properly otherwise, duh!"

I think by now it is fair to say that there is no such answer. If there are uses, then they are coding style related and 'debatable'.

I tried to avoid an 'opinion' discussion by countering the most common ones directly in the question, clearly that didn't work...

I was simply curious why Python has this feature, and thought there might be some best pratice for me to learn.

Since everybody else had a go at their opinion, let me add my own:

I advise against unbinding local variables. Less experienced Python programmers (especially those with a C++ background) are likely to missinterpret del x as delete the object referenced by x. They often treat del x like a short-cut for x.__del__(). Many use __del__ far too often too. I've seen this misconception many times. What makes it so bad is that it works for them initially! With simple code, where the object is only referenced from that variable, CPython behaves like they think it should. So beginners get positive reinforcement on their misconception and start using it more and more intensively, ending up with a ton of buggy code and weird behaviour that they don't understand.

Community
  • 1
  • 1
Stefan
  • 4,187
  • 1
  • 32
  • 38
  • Too broad of a question. – Tymoteusz Paul Oct 17 '14 at 01:16
  • 1
    Not if the answer is _no_. – Alexander Gessler Oct 17 '14 at 01:17
  • @Puciek: Just name one real use-case for this. I'm not asking for all of them. – Stefan Oct 17 '14 at 01:17
  • 2
    SO is not about "name one real use case", this is for specific programming questions and problems, not "post as many answers as possible". – Tymoteusz Paul Oct 17 '14 at 01:18
  • 1
    @Mephy it's is very broad, it is also opinion based and it is one of those "curious" wiki-type questions that doesn't solve any actual problem. – Tymoteusz Paul Oct 17 '14 at 01:22
  • 2
    ["*You should only ask practical, answerable questions based on actual problems that you face.*"](http://stackoverflow.com/help/dont-ask). Unless OP is designing a new programming language, this doesn't strike me as a practical question based on an actual problem. – Robᵩ Oct 17 '14 at 01:27
  • What is meant by saying `x = None` should "have the effect" as garbage collection? `del x` will cause a decref on `x`, not necessarily sending the ref count to zero, [as the note in the docs says](https://docs.python.org/2/reference/datamodel.html#object.__del__). If `x` has important operations in `__del__` then this distinction (and the timing of that execution) could matter and could be useful (destroying a temp file, or otherwise doing some special resource handling comes to mind). – ely Oct 17 '14 at 01:28
  • 5
    Actually, there is one answer in the link that you gave in your question. The *intent* of `del x` is clear, whereas the intent of `x = None` is not. With `x = None`, I don't know whether that was simply done for garbage collection or the code actually requires `x` to be `None` later on. – wookie919 Oct 17 '14 at 01:30
  • 1
    @EMS - What is meant is that `del x` and `x=None` are indistinguishable w.r.t. the lifetime of the object to which `x` previously referred. In either case, all that happens is a decref. – Robᵩ Oct 17 '14 at 01:30
  • Ah, I see. Then I would say it just comes down to an opinion about whether `x = None` is obfuscating the intention to decref and induce garbage collection. It's standard reasoning that this is obfuscated, but still "an opinion" for purposes of closing the question. I think the q&a at the question that the OP links more than covers everything here. – ely Oct 17 '14 at 01:32
  • I think the reason for `del x` is not for garbage collection but for sanity checking and exposition. To me `del x` says any use of x after this point in its defining scope is an error. (Redefining a new x is another matter, and it will be in a new scope.) – Michael Anderson Oct 17 '14 at 02:19
  • 1
    `del x` done at the global level can be used to remove a name from a module. If you have a module with a temporary variable `x` then `x = None` will leave that variable visible in `dir(module)` and accessible as `module.x`, which sort of makes it part of the module's API. `del x` fixes that. – John Kugelman Oct 17 '14 at 03:20

1 Answers1

0

I think the case you dismiss in your question is a valid place where semantically it makes more sense to use del.

There are other places where del can't be simply replaced. eg.

Deleting every second element of a list

>>> a = range(10)
>>> del a[::2]
>>> a
[1, 3, 5, 7, 9]
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 2
    The question specifically asks for the use of deleting a *variable* (`del x`), and not deleting from an aggregate, which is adequately covered by the other question OP linked. – hobbs Oct 17 '14 at 01:50