The Most Common Mistakes That Python Developers Make


Python is a powerful general-purpose programming language. It is a globally accepted language used in server-side web development, data science, and software development, system scripting, and creating software prototypes. Python is an interpreted, object-oriented, high-level programming language.

Python works on different platforms such as Mac, Linux, Windows, Raspberry Pi, and more. Python is a simple language, but there are several mistakes that programmers make, says Jack, a programming expert who works at TopAssignmentExperts. Let us learn about the most common mistakes made by python developers.

1. Specifying parameters incorrectly for an exception block

Consider the following code:

>>> try:

… l = [“x”, “y”]

… int(l[2])

… except ValueError, IndexError: # To catch both exceptions, right?

… pass

Traceback (recent call):

File “<stdin>”, line 3, in <module>

IndexError: list index out of range

The problem with this code is that the “except” statement here does not make a list of exceptions specified in the same way. If you can Python 2 developer, you can make this sort of mistake. As a result of this error, the IndexError exception is not being caught by the “except” statement. This exception ends up being bound to a parameter named IndexError. The proper way to catch multiple exceptions in an “except” statement is by using tuples and use as to bind to a parameter. This code works for both Python 2, 3.

>>> try:

… l = [“x”, “y”]

… int(l[2])

… except (ValueError, IndexError) as e:

… pass


2. Using class variables wrongly

In Python, class variables are privately managed as dictionaries and obey MRO. Consider the following example:

>>> class X(object):

… a = 1

>>> class Y(X):

… pass

>>> class Z(X):

… pass

>>> print X.a, Y.a, Z.a


1 1 1

Now let

>>> Y.a = 2

>>> print X.a, Y.a, Z.a

1 2 1


>>> X.a = 3

>>> print X.a, Y.a, Z.a

3 2 3

It causes a Python problem. It happened because attribute a was not found in class Z. So, Python looked for it in base class X.

3. LEGB Rule

The Python scope is presented using the LEGB rule. The letters in the acronym LEGB stand for Local, Enclosing, Global, and Built-in scopes. Let’s take a quick overview of these terms.

  • Local (or function) scope is the code block or body of any Python function or lambda expression. This Python scope contains the names that you define inside a function. These names will only be visible from the code of the function.
  • Enclosing (or nonlocal) scope is a special scope that only exists for nested functions. If the local scope is an inner or nested function, then the enclosing scope is the scope of the outer or enclosing function. This scope contains the names that you define in the enclosing function.
  • Global (or module) scope is the top-most scope in a Python program, script, or module. This Python scope contains all of the names that you define at the top level of a program or a module. Names in this Python scope are visible from everywhere in your code.
  • Built-in scope is a special Python scope, created or loaded whenever you run a script or open an interactive session. This scope contains names such as keywords, functions, exceptions, and other attributes built into Python.

4. Indentation Error in Python

Python is a procedural language where the code is arranged through whitespaces. The indentation error occurs when these spaces or tabs are not placed correctly, says Max, who provides online courses that you can order using the buy coursework online link. However, there is no particular syntax for indentation. Consider the following example:

for i in range(1,10):

print( i)

if i == 6:


This code won’t work because of the space in print command. Write print(i) without any space in between to run the program.

5. Misusing The __init__ Method

Consider the following code:

class Labour:

def __init__(self, name, workexp): = name

self.workexp = workexp

self._avgsal = workexp*1.5*100000 # causes “TypeError: __init__() should return None”.

return self._avgsal

emp = Labour(“Python Programmer”, 10)

To solve this problem, you can move the desired logic to a different instance method.

class Labour:

def __init__(self, name, workexp): = name

self.workexp = workexp

self._avgsal = workexp*1.5*100000

@property # Relocated the logic for returning work exp to a new method.

def getAvgSal(self):

return self._avgsal


emp = Labour(“Python”, 100)


6. Misusing expressions

Misusing expressions as defaults for function arguments is a type of error that is hard to debug. Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value. It happens when we can specify a mutable optional function argument, says Paul, the python expert who provides python assignment help services. For example:

>>> def append(element, range=[]):


return range

The output to this is:

>>> append(1) # range is assigned to [] [1] # This returns a reference the *same* list as the default for `range`

>>> append(2) # => `range` is now given [1] as a default!

[1, 2]

which is not expected.

Here is how we can address this problem:

>>> def append(element, range=None):

if range is None:

range = []


return range

The output to this code will be as expected.

>>> append(1) # `range` is assigned to [] [1]

>>> append(2) # `range` is assigned to [] again!



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.