7 Mistakes Every Python Coder Should Avoid

Python mistakes

Python is one of the top choices of programming languages for beginners. It is easy to learn, easy to use and can be used for high-level programming. However, once starting to code with Python, one might make avoidable mistakes. 

Today, we list some of the Python features and mistakes that are best avoided: 

Case sensitiveness 

Python is case sensitive, which means, a letter in cap will mean completely different for a variable with the same name but in a small case. For instance, ‘num’ and ‘Num’ are two different variables. It is one of the most common errors made by developers beginning to programme in Python. 

THE BELAMY

Sign up for your weekly dose of what's up in emerging technology.

Python offers functions that perform the same function, but the execution is different. 

For instance, functions sort() and sorted() perform the same task (sorting of arrays), but work differently: 


Download our Mobile App



Code:

list1= [7,9,5,2,1] 

print(list1.sort())

list2=[8,2,3,4,9,1]

list2=sorted(list2)

print(list2) 

Output: 

[1,2,3,4,8,9]

While list1 was sorted in the first line of code, it did not display any output since the sorting was done in place. Whereas in the second line of code, a new list is created that prints the sorted result. 

Thus, programmers should be familiar with how each function works in order to get the desired results. 

Not using comments and Doc strings 

In any programming language, comments form an integral part. The basic job of comments is to make the code self-explanatory and more readable. Docstrings, on the other hand, is a string literal specified in source code, such as a comment, used to document a particular segment of code. 

For a long code, no use of comments or doc strings will lead to challenges during debugging. Comments and doc strings can come as a lifesaver to the programmer. 

Low test coverage 

When starting with Python programming, one may not be familiar with the advantages of unit testing. However, once past the ameateur stage, programmers realise the necessity of unit testing. When working on untested code, one might introduce a new error while trying to fix another. Therefore, it is advisable to use test cases and validate implementations as soon as one adds a new feature or a function. Libraries such as unittest and pytest are available for this. 

Inappropriate Naming 

In order to make the code more readable and error-free, a programmer must follow the convention of appropriately naming the code. 

That is, 

  • Naming functions carelessly may result in blunder. For instance, a function that displays the minimum value should be named as “returnMinimumValue()” instead of “min()”. 
  • Names of models should not clash with Python standard library modules. This may lead to problems or become challenging when one is trying to import the library module. 

Mistakes in Indentation 

Python does not rely on brackets to separate code blocks. Thus, even one indentation mistake can result in unexpected output. This becomes all the more challenging when one is using multiple functions– all in the same code file. Oftentimes, it may not even give an indentation error but can become a serious bug in the code. 

Misusing the __del__ method 

In Python, __del__ is a dunder or magic method. Magic methods are ones with two prefixes and suffix underscores in the method name. It is usually used for operator overloading. 

__del__ is a destructor method and is called when all references of an object are deleted. That is, when an object is garbage collected. 

Syntax to use __del__: 

def__del__(self):

Body of destructor

.

.

Misusing the __init__ method 

Init is a reserved method in Python. The __init__ method can be used when an object is created from the class and access is required to initialise the attributes of the class. That is, it is used to set the values of instance members for the class object. However, oftentimes programmers use it to return a value from the init method– not the actual purpose of the method. 

Support independent technology journalism

Get exclusive, premium content, ads-free experience & more

Rs. 299/month

Subscribe now for a 7-day free trial

More Great AIM Stories

Debolina Biswas
After diving deep into the Indian startup ecosystem, Debolina is now a Technology Journalist. When not writing, she is found reading or playing with paint brushes and palette knives. She can be reached at debolina.biswas@analyticsindiamag.com

AIM Upcoming Events

Early Bird Passes expire on 3rd Feb

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
AIM TOP STORIES

All you need to know about Graph Embeddings

Embeddings can be the subgroups of a group, similarly, in graph theory embedding of a graph can be considered as a representation of a graph on a surface, where points of that surface are made up of vertices and arcs are made up of edges