0
why is there a "del" function when your code could be edited later on?
for example: >>> spam = 2 >>> eggs = 3 >>> del spam >>> eggs = 4 >>> spam = 5 >>> print(spam * eggs) i know the answer is 20, though i dont understand why there is a "del spam" option
4 Réponses
+ 13
to get it out of memory, although I guess that doesn't matter as much with pythons garbage collection
but really, del is useful for items in lists and dictionaries. there are things I couldn't have done if del didnt exist
+ 4
You sometimes need to annoy your programmer friends... It's when you need it(Just kidding. Sometimes you don't need it anymore so you delete it)
+ 4
Think embedded (Pi, Arduino, budget mobiles, Internet of Things)...limited resources...the reality for numerous Android users. SoloLearn's Python memory is artificially ~1MB.
Variables reference memory. Garbage collection frees the memory when the memory's *reference count* reaches 0.
https://docs.python.org/2.5/api/refcounts.html
A commented program showing reference count changes:
https://code.sololearn.com/ct7ToIVRI24y/?ref=app
(In your example I did not check if Python reuses spam's reference if it's re-set before GC. That seems inefficient so I suspect not)
+ 1
In some python editors, you do not have the ability to change your code, once you enter the next line. Hence. ........