If python uses latest value of a variable then why we need del command? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

If python uses latest value of a variable then why we need del command?

23rd Oct 2020, 3:44 AM
Ujjwal Sharma
3 Answers
+ 2
imagine you have a carton box, and you use it when you are moving from one apartment to another. Now the movement process has done. You don't need that box anymore. But it exists and takes valuable place in your new home, which could be used for another useful staff like: chair, table, sofa, tv etc. What we gonna do then? Obviously we throw it away. And the action to throwing away that box is performed by del. Hope it clear your doubts
23rd Oct 2020, 4:19 AM
Shadoff
Shadoff - avatar
0
Thank you both the answers are best for me
23rd Oct 2020, 5:29 AM
Ujjwal Sharma
0
I would say that the del keyword behaves differently in different context. When you use it on variables, it deletes the variable, and decrease the ref counter of the object it was linked to. That means that the object is not necessary destroyed, since it's ref counter must reach 0. When applied on dictionaries (del a[b]) for instance, it does not behaves exactly the same : it calls the __delitem__ magic method of the dictionary, which will delete the item accordingly.
23rd Oct 2020, 5:39 AM
Théophile
Théophile - avatar