7 Common Mistakes Python Developers Should Avoid

7 Common Mistakes Python Developers Should Avoid

Python is considered as one of the languages with the simplest syntax, along with the huge applicability. There are some common mistakes that developers make when working with the language. Here are some of the most common mistakes that Python developers make.

1. Error handling

Errors in Python have a very specific form, called a traceback. When you forget a colon at the end of a line, accidentally add one space too many when indenting under an if statement, or forget a parenthesis, you will encounter a syntax error. This means that Python couldn’t figure out how to read your program.

2.Incorrect Indentation

To indicate a block of code in Python, each line of the block has to be indicated by the same amount.

In Python, unlike other languages, indentation means a lot more than making code look clean. It is required for indicating what block of code a statement belongs to. Many characteristics depend on indentation. Some indentation errors in Python are harder to spot than others. For example, mixing spaces and tabs can be difficult to spot in a code. If that happens, whatever is seen in the editor may not be the thing that is seen by the Python when the tabs are being counted as a number of spaces. Jupyter notebook automatically replaces tabs with spaces, but it gives an error in most cases. To avoid this mistake, for the entire block, all spaces or all tabs must be used.

3.Misusing The __init__ Method

The init is a reserved method in python classes used as constructors. In object-oriented terminology, it is called as a constructor And it gets called when Python allocates memory to a new class object. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class. The objective is to set the values of instance members for the class object. Trying to explicitly return a value from the init method implies that the user wants to deviate from its actual purpose.

4. Class variables use

In Python, class variables are internally handled as dictionaries and follow what is often referred to as Method Resolution Order or MRO. It defines the class search path used by Python to search for the right method to use in classes with multi-inheritance. This causes a Python problem unless it’s handled properly. Consider the following example:

class A:

def rk(self):

print(” In class A”)

class B(A):

def rk(self):

print(” In class B”)

r = B()


the methods that are invoked is from class B but not from class A, and this is due to Method Resolution Order(MRO). The order that follows in the above code is- class B – > class A.  This causes a Python problem unless it’s handled properly.

5.Variable Binding

Python has late binding behavior. There is often confusion among Python developers regarding how Python binds its variables. What it means is that it binds its variables in closures or in the surrounding global scope and hence the values of variables used in closures are looked up at the time the inner function is called.

6. Python Standard Library Module Names

Python is rich with library modules which comes out of the box. A common mistake is using the same name for a module and a module in the Python standard library. This will lead to importing another library which will then try to import the module in the Python standard library and since there will be a module with the same name, the other package will import the defined module, in place of the Python standard library module.

7. LEGB Rule

Python scope resolution is based on what is known as the LEGB rule, which is shorthand for Local, Enclosing, Global, Built-in. Python uses a different approach for scoping variables than other programming languages. If a user makes 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 an outer scope. This error is particularly common for developers when using lists.

Source: analyticsindiamag