+ 1
Overwriting variables
So you can change the value of a variable without using del?
3 Answers
+ 5
del doesn't necessarily free the memory.
First of all it erases a NAME.
a = [] # We create a list
b = a # We give it a second name
Now we can use the same list by calling it a or b in our code.
del a now deletes the name a, but not the list. It's still there, in the same place, and you can still use it with the name b.
del b
Now the second name is also erased.
And now, since nothing refers to b anymore, the garbage collector of Python deletes the actual object for you.
+ 3
Whoops - the other answer is gone?
Sam Hatchard, you can just overwrite a name, even with different types, there won't be a problem.
a = 1
a = 5
a = [6, 7, 8]
The actual creation and deletion is done for you automatically.
So assigning a value to a name is more like pasting a name sticker onto an object, then later pasting it somewhere else.
+ 1
more like overriding variable