Common Pitfalls in Python Programming

When programmers being coding in Python or come from a different programming language they often tend to make numerous mistakes while coding in Python. Some of those are as follows:

  • Using Loops instead of using list/dict comprehensions
  • Using incorrect data structures
  • Defining classes incorrectly
  • Handling files incorrectly

We will go through these one by one

Using Loops instead of using list/dict comprehensions

Most programmers new to Python write the following code:

numbers = [1, 5 , 3, 6]
squared_numbers = []
for i in numbers:
square_of_number = i**2
squared_numbers.append(square_of_number)

The above piece is not wrong but a better way to write this in Python is:

squared_numbers = [number**2 for number in numbers]

This is the beauty of Python. Various banal things get simplified by using appropriate constructs in Python.

Using incorrect data structures

Python has various data structures for various purposes. Some of them are listed below:

  • Lists — Array of elements
  • Sets — Array of elements optimized used mostly for operations such as union, intersection, diff etc.,
  • Dictionaries — Key-value pairs for lookup based on key
  • Tuples — Immutable and ordered array of elements

One of ways programmers misuse lists is to use it for checking existence of elements. This works fine from a functionality point of view however, the right data structure to use for such an operation is a set. This holds true especially when the number of elements is large.

Defining Classes Incorrectly

Here is one way classes are defined by many novice Python programmers:

# Example 1
class SomeClass:
pass
>>type(SomeClass)
<type 'classobj'>

Although this is not incorrect(and doesn’t have any issues with Python 3) there are some subtle issues with this kind of definition. Consider this definition:

# Example 2
class DifferentClass(object):
pass
>>type(DifferentClass)
<type 'type'>

When a class inherits from ‘object’ base class in Python 2, lot of metadata is set automatically. However, the class defined in Example 1 doesn’t have much metadata. Additionally MRO is not supported with the former styled definitions.

Handling Files Incorrectly

Most often I see files opened and closed explicitly. This is an anti-pattern in Python. Python provides built-in context managers for that purpose. Consider the following snippet:

f = open('somefile.txt')
# read the file
f.close()

The above code works fine however, what happens when some unhandled exception occurs between opening and closing the file. The file pointer would have a dangling reference. The above code can be refactored to the below:

with open('somefile.txt') as f:
# read the file

The above code will handle any exceptions that occur within that block and properly close the file.

--

--

Prasanna Sagar Maddu
Building Enterprise Applications in Python

Vice President — Data Scientist Leader, Data Strategy, Applied ML and Machine Learning/Deep Learning/Python Expert