7 Common Errors While Coding In Python Which Needs To Be Avoided

Shubham Mishra
MobileAppDiary
Published in
4 min readAug 11, 2020

When it comes to Python, what comes in your mind? It is a user-friendly high-level, object-oriented, functional, imperative, and procedural programming language. Well, that is true, with its learning span, not that time-taking and the fact that it uses simpler variables and functions, many coding beginners opt for Python instead of any other programming language.

Now beginners are prone to mistakes, and with Python, which comes with probably the simplest syntax, to go with a higher ratio of applicability, mistakes are bound to happen. In this comprehension, I am going to discuss some of the commonly made mistakes made by the developers while working on Python.

  1. Error handling

Well, this one is among the basic errors one could commit while writing code in Python. The basic errors come in the form of:

  • Syntax Error- When you omit a colon, forgot parenthesis, add more than one space.
  • Recursion Error- Related to stacks and occurs when you call a function.
  • Out of memory Error- Depends on system RAM
  • Exceptions- Errors that are unconditionally fatal and detected during execution.

How to overcome: You need to ensure your syntax is error-free, which needs to be checked before the execution of the code.

2. Modifying a list while iterating:

This error occurs when you delete an element from an array or list while iterating it.

For those who are unaware of iterating, in Python, iteration is further divided into iterable and iterator. An iterator is an object which comprises a countable number of values. Technically, an iterator is an object which implements the iterator protocol.

While you delete an element from an array or list while iterating over, you tend to commit an error. Consider this with an example below;

>>> foo = lambda x : bool(x % 2)

>>>nums = [n for n in range(10)]

>>> for i in range(len(nums)):

… if foo(nums[i]):

… del nums[i] # Deleting item from a list while iterating over it

This results in an index error getting raised.

How to overcome: To avoid these types of errors, List comprehensions are critical.

3. Importing modules recursively:

This could be one of the major problems if you are importing a module in a loop. This may cause a circular dependency in your program.

Let us understand this with an example. Suppose you have two files def.py and abc.py;

In def.py:

import edef f():return e.aprint f()

In abc.py:

import da = 1def g():print d.f()

Here, there is a clarity which module is attempting to access which variable or function in the other. Problems are bound to take place in such a scenario.

How to overcome: To avoid these types of errors, do not overlap the importing of modules, which will prevent recursion.

4. Neglecting Python scope rules:

For someone coming from a C or C++ background, this could be a thing of trick. Python has a different approach when it comes to scoping variables. It allows accessing variables declared in the ‘if’ statements from outside or inside the loops.

Python scope resolution rule is based on the LEGB rule, which stands for Local, Enclosing, Global, and Built-in. This error is more prone to happen if the developer is using lists.

How to overcome: While using lists, the developer should be more attentive, as it is the only way Python scope rule is explored to errors.

5. Indentation:

Indentation is a lot more than just making the code look clean. During the indentation of a particular single block, the spaces, the tabs should never get mixed. If this happens, the impression in the editor may not be the thing that is seen by Python, wherein, the tabs will be counted as the number of spaces.

How to overcome: The only way to avoid this problem is to use all the tabs or spaces for the entire block.

6. Misusing the __init__ method:

In object-oriented terminology, the __init__method is called a constructor, whereas, in Python, it is a reserved method that can is more likely or close to a constructor.

This method gets called when Python allocates memory to a new class object. The premier objective of this method is to set the values of instance members from the class object. While trying to return a value from the __init__methodit explicitly implies that the user wants to deviate from its actual purpose.

How to overcome: The only solution to overcome this issue is to add a new property and move the desired logic to a distinct instance module.

7. Name clashing in Standard Library modules

Python has a significantly large number of library modules. However, there might be clashing of names when as there are some standard name modules in it.

With the same names, there are chances you might end up importing another library, which in return, will try to import the module in the Python standard library. Since there will be a module of the same name, a clashing will appear, and the package will import the defined module instead of the Python library module.

How to overcome: There are no such measures to overcome this issue. The only way out is to avoid using the same names as that of standard Python libraries.

Conclusion:

For many startups, Python is the ‘go-to’ programming language, and this is because it offers the simplest variables, functions, and syntax yet able to solve complex problems and works better in the AI environment.

However, sometimes with the limited understanding of its capabilities might work against you. If these common mistakes are addressed with better understanding, the final product is assured of being error-free and impactful.

--

--

Shubham Mishra
MobileAppDiary
0 Followers
Writer for

Creativity comes from a creative mind and i believe words are the most influential things that weilds the power of constructing a positive ecosystem.