Earlier in our blogs we have brought for you top 5 Python certification courses and Python Project for Beginners. Also, different types of games that you can build using Python. For instance, helicopter and car game, Pong game and flappy bird. So, here is our new blog on common mistakes Python developer should avoid.

Python is a multi-function programming language. This is because, contrary to, other languages like HTML, CSS, JavaScript etc., it can be utilized for other kinds of programming. Due to the strong growth of software development, the popularity of Python has heightened. It is comparatively easy to use than Java and C which is why Python is perfect for beginners. Sometimes beginners take an easy way out for the problems they face. They use easy methods to make the code work. But working code does not always means that it is the right way. So here we bring you some of the common mistakes Python developer should avoid:

Top 4 Common mistakes Python developer should avoid are as follows :

1:Inaccurate Indentation

When there is a code block in Python, every line of the block must be shown by the same amount. In contrary to other languages, in Python, Indentation has a wider meaning. It is not just for making the code look clean but points out that the statement belongs to which block of code. One of the examples of Indentation error is mixing spaces and tabs. For example, if you mix a tab or a space together it is difficult to point out. Whatever you see in editor is not seen by python when tabs are being counted as spaces. Notebook Jupyter replaces tabs with spaces but gives out a lot of errors. So, to prevent this from happening use all tabs or all spaces for the entire code block.

2:    Proper use of Class variables

In Python, class variables are used as dictionaries and MRO (Method Resolution Order) is followed. It gives the class search path used by python to find the right classes used in multi-inheritance. This can cause problems to python unless handled properly. For example

class A:
   def rk(self):
       print (” In class A”)
class B(A):
   def rk(self):
       print (” In class B”) 
r = B ()
r.rk ()

The code is followed in the order from B to A due to Method resolution Order (MRO). And this causes a problem in the python.

3: globals Use case:

Variable that are created outside of a function are called Global variables. Global variables are both used inside and outside of a function. A local variable that is defined for the block and is only available for that block. It is not accessible outside the block. The use of Global variables allows you to change the global function locally without any problem. For example:


a = 1 # a variable    

def increment():
    a += 1
    return a

def increment2():
    global a # can make changes to global variable "a"
    a += 1 
    return a
  
increment()  # UnboundLocalError: local variable 'a' referenced before assignment
increment2() # returns 2

As a beginner you love using global it saves you from passing all the arguments you need to pass for the function. But guess what you are in danger of an error.

While global variables can be changed by any part of the code making it difficult to remember or reason of why and where it was used. No access control while using global variable. Global variable causes tight coupling of codes and it can cause namespace pollution.

4:Not Using mutable objects properly:

This can be a surprise for all the new python learners out there as this is quite a new thing in this language.

Two kinds of objects in python.

Mutable objects are those objects that change their content without changing their identity.Whereas immutable objects are those which can’t be changed. Immutable objects can be int, float, string, bool, and tuple which are built in.And there are mutable data types like listset, and dict. So, you can change the contents of list, for example: list_[2] = ‘abed’

When the arguments are immutable. And when the function is called twice without feeding the value for the list it takes the default value. A new empty list would be created if second list argument is not given.

It means default arguments are only evaluated once the function is defined. This means calling on the function again and again would not change the default arguments.

So, a default argument is mutable and is changed every time function is called. The fix we have for this is to use None as the default value.   

Conclusion:

Here are some common mistakes python developers Should Avoid. We are here to give you the head start as we learned it the hard way you can learn through this blog in a simple and effective way.