+ 1

Doubt about del statement in Python

If a string variable is deleted with the del statement, what happens to the string contained in the now deleted variable?

29th Jun 2024, 11:32 AM
Giovanni Paolo Balestriere
Giovanni Paolo Balestriere - avatar
2 Antworten
+ 3
del deletes the reference. When the value is garbage-collected (deleted), depends on the implementation. It may be erased right away or later – given that the data is not referenced otherwise, of course. https://stackoverflow.com/a/1481512
29th Jun 2024, 12:01 PM
Lisa
Lisa - avatar
0
I never found the need to del a string variable. Using del on a variable makes it invalid unless a new value is assigned to it. Python is garbage collected, so memory allocation and deallocation is abstracted away. The more common use of del is for list items. courses = ["Python","Java","C++"] del courses[1]
30th Jun 2024, 5:05 AM
Bob_Li
Bob_Li - avatar