Seven Python mistakes all beginner developers make - and How to avoid them














































Seven Python mistakes all beginner developers make - and How to avoid them




Seven Python mistakes all beginner developers make - and How to avoid them.




You must have heard a famous quote about Python , ' Life is Short , You need Python '. I was just thinking about the time when I actually started programming in python language much more expectations than the previous languages like C , C++ . Though I had a good command in C and C++ languages till then but starting a new journey is always challenging . Then I understood a very interesting fact that finding the root of an issue is not an easy task in Python . The fix of your error may takes upto whole day if you are a beginner.

Does this sound familiar ? I think everyone has this kind of self challenging problems but it is always better to learn from yours and others mistakes. So I decided to gather some of the most common mistakes beginners make while using Python which may save you countless hours of debugging . I hope this article will help you if you have just started programming using Python.

1 . Functions with side effects (list argument):

Since Python is not purely functional like Clojure , the functions may cause side effects . This is not really a mistake but you have to know its consequences before using anything. Look at the example below to understand the issue .




Objects are reference type in Python and if you pass lists as an argument , a reference is passed instead of its value . You can also relate it from the C and C++ Array passing . Whatever you change in the list you make inside the function , it will reflect the change into your list. This can lead some of the very nasty consequences and hours of debugging.

2. Functions calls in default arguments:

Default arguments must be used carefully. Take a look to the following example.







If your function has default arguments, the function automatically assign the value when we do not pass any argument related to that data type. The reason is that Python evaluates the expressions in default arguments when the function is defined. But if you want to generate arguments dynamically then you can do the following.




3. Mutable default arguments :

This problem basically the combination of the above two . You will understand the issue if you see the example.



There are two important points you should note:

  • Python evaluates the expression [] when the function is defined. (This is same as calling list() method)
  • The reference of this object is bound to the argument. Thus, whenever you call the function, the same object will be used again and again.

    However, using mutable objects as default arguments is not a good idea. You should never ever do that unless the problem says that.

4. Unintended refrence assignments :

This one is most common mistake users make . As we already covered every object is a reference type in Python. You better mind it while creating a copy of a list. See the following example to understand this .




When you execute b = a, you actually store the reference of a into b .In C and C++ , this actually copies whatever the data present in variable a into the variable b. But in Python it just stores the reference of a to b. Thus, a and b points to the same object. But if you want to make an original copy of a list , you should use the built-in deepcopy() function. This recursively copies all value type attributes to the new variable.




This is also important to take care because if you try this b = a into integers everything behaves as you expect because it just overwrite the reference .

5. Using from something import* :

We all must have done this thing importing a module from something. There are some drawbacks of doing this.

functions in different namespaces can have the same name, causing confusion throughout your code base.
when you import a module in Python, all code in that module will be executed which is just unnecessary to do . This can significantly slow things down if there are lots of submodules to import even if they are never used throughout the program . So, if you only import NumPy to generate random numbers, you are probably better use

from numpy.random import random


instead of

import numpy as np



6. Joining paths with string concatenation :

Suppose that you have to open a file called data.csv from the folder given by the variable data_folder. How should determine the file path? If you are doing simple string concatenation

data_path = data_folder + "/data.csv"


Then it is not right . This may work on Windows sometimes for instance . The developers in the field of Python must have experienced these things.
Coming upto the solution , you should use pathlib , Python in built tool or use os.path.join() method.

data_path = os.path.join(data_folder , "data.csv")



7. Low test coverage :

This is a high level issue. Especially when you are a beginner, the benefits of unit testing is really ignorable until you experience an expertise. However, every seasoned developer can tell you that it is absolutely essential . Working with untested code is like messing up with a Whack-A-Mole game: Fix one bug and introduce another .

Avoiding this starts from the very beginning of your project. As soon as you add a feature (or a function even), you should write test cases including the boundary conditions to validate the implementation. There are several great libraries in python for this, for example the built-in unittest or the very popular pytest.

You should seriously invest your time into testing your code(suggested your code should have better readability). It might take some time to do that, but it is a long term investment. You will save much more time of debugging which obviously you do not want to.

So, In brief these are some most common mistakes which most of the beginners worry about and learn from them . I hope this article helps you when you are debugging your code regarding functions, list and reference in Python.

Thank You !