0

Learning Python at the moment. (coming from PHP) In Python there's a delete(del) function to delete your variables. This is new to me,

What are the benefits of deleting a variable?

What are the drawbacks of not deleting a variable?

In what situation would I most likely want to delete a variable?

EDIT: This question here just compares the differences between del var and var = none. I'm trying to understand why you would need to delete a variable in the first place.

Community
  • 1
  • 1
Trevor Wood
  • 2,347
  • 5
  • 31
  • 56
  • 1
    Do you know what it means to ```del``` something? – wwii Mar 01 '16 at 03:31
  • @wwii to remove it, but for what reason would I need to remove it besides wanting to use the name of the variable again? – Trevor Wood Mar 01 '16 at 03:33
  • @ChrisC73 Thanks for the link chris, but it just compares del and none. Doesn't explain why you want to delete a variable – Trevor Wood Mar 01 '16 at 03:38
  • 2
    You don't need to delete a name to assign something different. Read the definition of [The del statement](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement), [Names and Objects](https://docs.python.org/3/tutorial/classes.html#a-word-about-names-and-objects), [Scopes and Namespaces](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces), and [Naming and Binding](https://docs.python.org/3/reference/executionmodel.html#naming-and-binding) - I don't know PHP so I can't give you a contrast. Those last three are pretty important to understand. – wwii Mar 01 '16 at 03:40
  • 2
    @TrevorWood: Read the other answers. Several give reasons, though in practice, `del` of a plain name is rarely useful; unless you know no other references exist, it won't guarantee memory is even freed. Just because the question is more focused on `del x` vs. `x = None` doesn't mean the answers don't address your question. – ShadowRanger Mar 01 '16 at 03:41
  • @ShadowRanger I read through them. Basically what I got was, 1. Clean/DRY 2. Can be sure that variable is empty and not in use. – Trevor Wood Mar 01 '16 at 03:50
  • 1
    [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html). You ```del``` things to remove the reference to an object - garbage collection uses reference counting - there are a couple of good links in this [SO answer regarding garbage collection](http://stackoverflow.com/a/4484312/2823755). – wwii Mar 01 '16 at 03:55
  • 2
    @TrevorWood: Yup. That's about it. Only real reason to do it is if you know you hold the only reference to something huge, and you need that huge thing released _now_ (in CPython only; in other, non-reference counting interpreters, it's released "some time"). 99.9% of Python code has no reason to use `del` with plain names, the only serious use case is usually deleting entries in a `dict` (`del mydict['somekey']`) or removing elements from a sequence by index/slice (e.g. to delete even indices in `list` keeping odds only, `del mylist[::2]`). – ShadowRanger Mar 01 '16 at 03:56
  • @ShadowRanger ahh okay I see. So it's mainly used to remove big/heavy variables and/or values in a list – Trevor Wood Mar 01 '16 at 03:59
  • [Videos of Pycon talks](http://www.pyvideo.org/search?models=videos.video&q=garbage+collection) are a good resource. – wwii Mar 01 '16 at 04:05

1 Answers1

2

Although your question seems to point to same thought. But I have tried to respond in a way you asked it.

Benefits of deleting the variable It frees the memory occupied by that variable in the memory. After deleting the variable, the RAM can be used for other heavy tasks (like reading a file, loading an image, etc.)

Drawbacks of not deleting the variable If you don't delete the variables (say 10) initialized each with an image/video file then you can guess yourself that you can run out of memory. Now guess what if the videos were 5K then you will most probably run out of memory. This's were del will come to rescue your program.

Situation in which you might need to delete the variable When you are sure that the variable will never be required in future. Or, again, when you might get into situations in which you may run out of memory.

Rahul
  • 117
  • 1
  • 8
  • Thanks Rahul, So as a general rule of thumb, I should regularly delete variables? – Trevor Wood Mar 01 '16 at 03:52
  • 1
    Not regularly. But only when required. – Rahul Mar 01 '16 at 03:54
  • 2
    @TrevorWood: Not at all. Variables are automatically cleaned up when a function exits. Unless you have some unusual reason they need to be cleaned up sooner, _never_ use `del`, it's just wasting time on stuff Python does for you. – ShadowRanger Mar 01 '16 at 03:58
  • 1
    @TrevorWood - its not quite that simple, names/references/bindings can *go away* when the name goes out of scope. – wwii Mar 01 '16 at 03:59
  • Ah okay, I see, so if I should just leave it alone if I'm not using it anymore. (and if it's not a big variable) Thanks guys! – Trevor Wood Mar 01 '16 at 04:03