Unused local variable in Python
A variable defined inside a function block or a looping block loses its scope outside that block is called ad local variable to that block. A local variable cannot be accessed outside the block it is defined.
NameError: name 'a' is not defined
Here we get an error because ‘a’ is local to the function func() and it loses its scope outside that block. Here the local variable ‘a’ is used to hold the number passed and it is utilized in the printing statement. But in some cases, the local variables wl be declared by assigning some value and left utilized for any process. Just assigning some value is not utilizing the variable, the value in it must be read and used for some computation.
Garbage collection is automatic in Python and when the reference count of an object comes to zero the object will be cleared. We all know that a variable local to a certain block of code loses its scope outside the block. The garbage collection is automatically invoked when the difference between the allocation of objects and the deallocation of objects crosses a certain threshold value. If the script is a few lines of code, then memory management is not a tedious process. If the script is very long with complex tasks and there are many unused local variables, it has a considerable effect on the code execution time and memory management. However, issues with memory management are very less.
The main issue that arises due to these unused variables is it decreases readability. When the code is thousands of lines, when another programmer is required to work with it, he may have to spend a long time analyzing what for the variable is used which is actually left unused. One may forget the unused variable declared and may use it in the later part which can result in undesirable output. It is always good practice to remove the unused local variables.
A code with many unused local variables, unused imports, or unused line of codes is considered to be dead code. When there are situations where you are ambiguous that a local variable may have a role later in the code, then there are some naming conventions that can be used to distinguish it from the rest of the variables. Naming the variable as ‘dummy’, ‘unused’ or any name that conveys that this variable is currently not used anywhere.
How to suppress the warning ‘unused variable’?
Some editors like VScode, PyCharm, Eclipse, PyDev will raise warnings sometimes regarding these unused variables. The warnings won’t interrupt the program execution. To suppress the warning, one can simply name the variable with an underscore (‘_‘) alone. Python treats it as an unused variable and ignores it without giving the warning message.
0 1 2 3 4
There are packages that can be used to find the dead code in the scripts. One such package is Vulture. The vulture package finds the unused local variables, unused imports, and unused cord parts and gives out the report. To install this package, type the following command in the anaconda prompt.
pip install vulture
Now type a python script and save it with .py extension. The file shall be stored either in the directory that opens by default in the anaconda prompt or in some specific location. In case of storing the file in some other location, one has to then move to that directory. Now consider the following script as an example. It is saved as trial1.py
0 1 2 3 4
Once the file is saved in the desired directory, then type the following command in the anaconda prompt to find the dead code.
The vulture package understands the naming convention explained below to avoid warning. Thus, when an underscore alone is used as a variable name, the warning message is suppressed. Consider the trial1.py, where the name of the unused local variable is changed to underscore(‘_’).