Insights from Our Experts
Seven most common mistakes that Python programmers make
Python is interpreted as a high-level programming language with its easy-to-learn syntax and dynamic semantics. It is widely used for rapid application development due to its large support of modules and packages and code reuse along with program modularity.
Python’s simple syntax may often mislead developers - especially if they are new to the language. This blog covers some of the top mistakes that even an advanced Python developer can make.
1. Modifying a list while iterating
Deleting an element from an array or list when iterating over it is a well-known Python problem for most of the developers.
This results in an IndexError getting raised. List comprehensions are particularly useful for avoiding this specific problem.
2. Weak error handling
The usage of exception handling always comes in handy. But, what if statements are not put inside a try-catch block when an unforeseen error occurs but was one which could have been bypassed or maybe just logged for later reference? The absence of an exception block in such a scenario will abort the execution without any informative message from the application.
Also, In cases where multiple exceptions are to be captured, if we write the following code:
The problem here is that the except statement does not make a list of exceptions specified in this manner. Instead, it has to write as:
The proper way to catch multiple exceptions is to specify the first parameter as a tuple containing all exceptions. Also, for maximum portability, use the “as” keyword, since that syntax is supported by both Python 2 and Python 3.
3. Circular module dependencies
Importing a module recursively may cause a circular dependency in your program. Suppose you have two files, abc.py and xyz.py, each of which imports the other, as follows:
And in xyz.py:
You will face some problems depending on the point at which each module is attempting to access functions or variables defined in the other.
4. Inconsistent indenting
In the indentation of a particular single block, the spaces and tabs should never be mixed unless the exact reaction between the system and codes is studied thoroughly. Otherwise whatever is seen in the editor may not be the thing that is seen by Python when the tabs are being counted as a number of spaces. So the best thing to do here is to use all spaces or all tabs for every block; the number to be used is entirely up to you.
5. Ignorance of Python scope rules
Python uses a different approach for scoping variables than other programming languages. For example, it allows accessing the variables declared inside loops or if statements from outside. It could be a bit confusing for someone coming from a C/C++ background.
Python scope resolution is known as the LEGB rule, which is shorthand for Local, Enclosing, Global, Built-in. You can have a well-formed idea of LEGB rule here.
The above program results in “ UnboundLocalError: local variable 'x' referenced before assignment.” The result is because, when you make an assignment to a variable in a scope, that variable is automatically considered by Python to be local to that scope and shadows any similarly named variable in any outer scope.
Read also: Python optimization techniques
6. Misusing The __init__ method
The __init__ methods are used as constructors in Python. It automatically gets called when Python allocates memory to a new class object. The purpose of this method is to set the values of instance members for the class object.
Trying to explicitly return a value from the __init__ method implies that you want to deviate from its actual purpose.
A solution for this is to add a new property and move the desired logic to a different instance method.
7. Name clashing with Python Standard Library modules
Python is abundant with its large number of library modules that come out of the box. But problems can occur if you run into a name clash between the name of one of your modules and a module with the same name in the standard library that ships with Python.
This will result in importing another library, which in turn tries to import the Python Standard Library version of a module. Since you have a module with the same name, the other package mistakenly may import your version instead of the one within the Python Standard Library.
Therefore, it should be taken care of to avoid using the same names as that of standard Python libraries.
Python is indeed a powerful language with many mechanisms that can greatly improve productivity. But having a limited understanding of its capabilities can sometimes be a hindrance. Familiarizing with the concepts of Python in much greater depth will surely help you overcome these common mistakes, and build a stable and scalable final product.