Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (7.3k points)

I can't really think of any reason why python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign None to it. And when deleting from a dictionary, a del method could be added.

Is there any reason to keep del in python, or is it a vestige of Python's pre-garbage collection days?

1 Answer

0 votes
by (106k points)
edited by

Below are the case were del becomes useful:-

You can use del to removes the binding of that name from the local or global namespace.

If you assign None to a name then it will not remove the binding of the name from the namespace.

Below is the piece of code that shows how we use del:-

del list_item[4]

del dictionary["alpha"]

You can also use del  for cleaning up extraneous variables in for loops:

for x in some_list:

     do(x)

del x

Once you have deleted the x you can see that x will be undefined if you use it outside the for a loop.

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
4 answers

Browse Categories

...