Is there a difference between setting a variable to None or deleting it?

Is there any noticeable difference in performance or memory if you clear the variables by setting

x = 'something' x = None 

Unlike

 x = 'something' del x 

for many variables?

+6
source share
5 answers

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! 
+7
source

Setting a variable to None or deleting it releases the reference to the x object. The main difference is that with x = None name x still exists, although now it points to another object ( None ). When using del name is also deleted.

There is no difference for the garbage collector. del does not cause garbage collection of the referenced object.

Edit: as @Viroide pointed out, using del throws an exception when trying to call the variable x .

+2
source

I was curious, so I decided to see what the dis module says by running this code:

 import dis def f(): v = 'x' v = None del v return None print(dis.dis(f)) 

Result

  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 docs say DELETE_FAST

Deletes local co_varnames[var_num] .

while LOAD_CONST

co_consts[consti] stack.

So, in terms of operations / memory, LOAD_CONST performs two operations (which use memory), and I think it only becomes relevant when you have a large number of elements (not just one), so you might want to use del .

This also explains why you can no longer access the variable v (since it was removed from the namespace, and LOAD_CONST is just another destination).

+2
source

Both operators differ in how they work with the current namespace: the first simply changes the value of the variable, and del completely deletes the variable. The previous value specified by x gets garbage collection anyway.

The differences in performance, if any, are minor.

+1
source

No, this is not the same, see 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 value of the variable None and has NoneType. But deleting a variable makes the variable inaccessible, removing it from the context.

0
source

All Articles