Python del statement/keywords

The Python del statement/keywords (also stands for delete) does the reverse of the assignment. Assigning a value to an object binds the object’s name to the address of the value. The address is the location in the memory where the value is held.

The del statement unbind the object’s name to the address. Note it does not delete the value or the memory of the object completely. This means the value still exists in the same address, except we cannot access it using the same name once the del statement has been called upon the object.

Here is an example.

>>> st='New string'
>>> #the name st is bind to the 'New string' string
>>> st
'New string'
>>> #we can access the string using the 'st' name of the object
>>> del st
>>> st
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'st' is not defined

As you can see once ‘del st’ is called, the name ‘st’ cannot be used to access the ‘New string’ string and we get an error message ‘NameError: name ‘st’ is not defined

The del statement can be used with any of the data type.

>>> st={23, 89 , 90 , 78}
>>> del st
>>> st
NameError: name 'st' is not defined

Some points to note about Python del statement

Some points to keep in mind while using the del statement.

i) When the object is in global or local scope

If the object is a global object you can call del upon that object inside the local scope using the global statement. If the ‘global’ stament is not used the deletion is not performed. And you will also get an error message something like this ‘UnboundLocalError: local variable ‘object_name’ referenced before assignment‘. The ‘object_name’ is the object’s name. Consider the code below .

Link :Python scopes type

>>> n=90
>>> def func():
...  del n
>>> func()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in func
UnboundLocalError: local variable 'n' referenced before assignment

From the error message it is cleared that ‘n’ has not been assigned any value or rather it is not accessible inside the function.

Let us try using the global statement and perform the deletion.

>>> n=90
>>> def func2():
...  global n
...  del n
>>> func2()
>>> n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

Now the deletion is performed.

Link : Python global statement

Deleting subscription

If the object holds a sequence of values which can be accessed using the subscript, then any of the value can be deleted using the subscript. In this case, the deleted value is dropped as the object’s value/itme and the value count is reduced.

A list is a data type which can hold sequence of values. So let us try deleting one of the values in the sequence.

>>> ls=[23 , 'text' , 90 , ['ks']]
>>> del ls[2] #delete the 3rd value
>>> ls
[23, 'text', ['ks']]
>>> ls[2]

After ls[2] is deleted the value in that index is dropped and the previous 3rd index value become the 2nd index value.

Note string does not support deletion of its item using the subscript. But of course we can delete the whole string.

>>> s=" is the best!!"
>>> del s[3]
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    del s[3]
TypeError: 'str' object doesn't support item deletion
>>> #We get error mesage 
>>> del s #Work fine
>>> s
 NameError: name 's' is not defined

Can we retrive the value the has been deleted using the del statement?

The answer to this question is “most probably not”.

Leave a Reply

Your email address will not be published. Required fields are marked *