Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, is use of `del` statement a code smell?

Tags:

python

del

I tend to use it whenever I am working on a prototype script, and:

  1. Use a somewhat common variable (such as fileCount), and
  2. Have a large method (20+ lines), and
  3. Do not use classes or namespaces yet.

In this situation, in order to avoid potential variable clash, I delete the bugger as soon as I am done with it. I know, in a production code I should avoid 1., 2., and 3., but going from a prototype that works to a completely polished class is time consuming. Sometimes I might want to settle for a sub-optimal, quick refactoring job. In that case I find keeping the del statements handy. Am I developing an unnecessary, bad habit? Is del totally avoidable? When would it be a good thing?

like image 892
Hamish Grubijan Avatar asked Dec 24 '10 21:12

Hamish Grubijan


People also ask

What is use of Del statement in Python?

The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.

What is the use of Del statement?

The del statement can be used to delete an item at a given index. It can also be used to remove slices from a list.

What does a Del Dict statement perform in Python?

Python's del statement is used to delete variables and objects in the Python program. Iterable objects such as user-defined objects, lists, set, tuple, dictionary, variables defined by the user, etc. can be deleted from existence and from the memory locations in Python using the del statement.

Should Del be used in Python?

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc.


1 Answers

I don't think that del by itself is a code smell.

Reusing a variable name in the same namespace is definitely a code smell as is not using classes and other namespaces where appropriate. So using del to facilitate that sort of thing is a code smell.

The only really appropriate use of del that I can think of off the top of my head is breaking cyclic references which are often a code smell as well (and often times, this isn't even necessary). Remember, all del does is delete the reference to the object and not the object itself. That will be taken care of by either reference counting or garbage collecting.

>>> a = [1, 2] >>> b = a >>> del a >>> a Traceback (most recent call last):   File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> b [1, 2] 

You can see that the list is kept alive after the del statement because b still holds a reference to it.

So, while del isn't really a code smell, it can be associated with things that are.

like image 77
aaronasterling Avatar answered Sep 21 '22 17:09

aaronasterling