Is there a noticeable difference in performance or memory if you clear variables by setting
x = 'something'
x = None
as opposed to
x = 'something'
del x
for many variables?
Is there a noticeable difference in performance or memory if you clear variables by setting
x = 'something'
x = None
as opposed to
x = 'something'
del x
for many variables?
Both times the memory for 'something'
will be reclaimed if x
was the last reference to that string object. In the first example, you use a tiny bit more memory because you keep the name x
around.
I did some searching and it looks like the memory will be reclaimed immediately: further reading.
There is a conceptual difference, however, and your program will behave differently. When you del x
, you unbind the name x
, and trying to use that name will raise 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
to be a name for the object None
, and can still use the variable as expected. For example for truthy/falsy tests:
>>> x = None
>>> if not x:
... print('hello cshin9!')
...
hello cshin9!
Setting a variable to None or deleting it both causes the reference to the object x
to be released. The main difference is that with x = None
the name x
still exists, even though it's now pointing to a different object (None
). When using del
the name is deleted as well.
For the garbage collector, there is no difference. del
does not force any garbage collection of the referenced object.
Edit: as @Viroide pointed out, using del
causes an exception to be thrown when trying to call the variable x
.
I was curious, so I decided to have a look at what the module dis says by running this code:
import dis
def f():
v = 'x'
v = None
del v
return None
print(dis.dis(f))
The result is
4 0 LOAD_CONST 1 ('x')
3 STORE_FAST 0 (v)
5 6 LOAD_CONST 0 (None)
9 STORE_FAST 0 (v)
6 12 DELETE_FAST 0 (v)
7 15 LOAD_CONST 0 (None)
18 RETURN_VALUE
and the documentations says that DELETE_FAST
Deletes local
co_varnames[var_num]
.
while LOAD_CONST
Pushes
co_consts[consti]
onto the stack.
So, in terms of operations/memory, LOAD_CONST
performs two operations (where one is using memory), and I think that it becomes relevant only when you have a high amount of elements (not just one) - therefore you might want to use del
.
This also explains why you cannot access the variable v
anymore (as it was deleted from the namespace, while LOAD_CONST
is just another assignment).
The two statements differ in a way they operate on the current namespace: the former simply changes the value of the variable, while del
completely removes the variable. The previous value pointed by x
gets garbage collected in any case.
Performance differences, if any, are negligible.
No, it's not the same, look at the following example:
x = "something"
x = None
type(x)
<type 'NoneType'>
z = "asd"
del z
type(z)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined
Setting to None makes the variable value be None and have NoneType. But deleting the variable makes the variable inaccessible by deleting from the context.