Both times, the memory for 'something' will be returned if x was the last reference to this string object. In the first example, you are using a bit more memory because you are saving the name x .
I did a few searches, and it looks like the memory will be fixed immediately: further reading .
However, there is a fundamental difference, and your program will behave differently. When you del x , you untie the name x , and trying to use that name raises a NameError :
>>> x = 'something' >>> del x >>> x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined
When you set x = None , you rebind x should be the name for the None object and you can still use the variable as expected. For example, for truth / fake tests:
>>> x = None >>> if not x: ... print('hello cshin9!') ... hello cshin9!
source share