+ 1
If python uses latest value of a variable then why we need del command?
3 ответов
+ 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
0
Thank you both the answers are best for me
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.