DEV Community

Cover image for 5 Python bugs that every developer is still facing in 2024 (and how to fix them)
Rupesh Sharma
Rupesh Sharma

Posted on • Originally published at dev.to

5 Python bugs that every developer is still facing in 2024 (and how to fix them)

Written by Rupesh Sharma AKA @hackyrupesh

Python, with its simplicity and beauty, is one of the most popular programming languages in the world. However, even in 2024, certain flaws continue to trouble developers. These problems aren't always due to weaknesses in Python, but rather to its design, behavior, or common misconceptions that result in unanticipated outcomes. In this blog article, we'll look at the top 5 Python issues that every developer still encounters in 2024, as well as their remedies.


1. Mutable Default Arguments: A Silent Trap

The Problem

One of the most infamous Python bugs is the mutable default argument. When a mutable object (like a list or dictionary) is used as a default argument in a function, Python only evaluates this default argument once when the function is defined, not each time the function is called. This leads to unexpected behavior when the function modifies the object.

Example

def append_to_list(value, my_list=[]):
    my_list.append(value)
    return my_list

print(append_to_list(1))  # Outputs: [1]
print(append_to_list(2))  # Outputs: [1, 2] - Unexpected!
print(append_to_list(3))  # Outputs: [1, 2, 3] - Even more unexpected!
Enter fullscreen mode Exit fullscreen mode

The Solution

To avoid this, use None as the default argument and create a new list inside the function if needed.

def append_to_list(value, my_list=None):
    if my_list is None:
        my_list = []
    my_list.append(value)
    return my_list

print(append_to_list(1))  # Outputs: [1]
print(append_to_list(2))  # Outputs: [2]
print(append_to_list(3))  # Outputs: [3]
Enter fullscreen mode Exit fullscreen mode

References


2. The Elusive KeyError in Dictionaries

The Problem

KeyError occurs when trying to access a dictionary key that doesn't exist. This can be especially tricky when working with nested dictionaries or when dealing with data whose structure isn't guaranteed.

Example

data = {'name': 'Alice'}
print(data['age'])  # Raises KeyError: 'age'
Enter fullscreen mode Exit fullscreen mode

The Solution

To prevent KeyError, use the get() method, which returns None (or a specified default value) if the key is not found.

print(data.get('age'))  # Outputs: None
print(data.get('age', 'Unknown'))  # Outputs: Unknown
Enter fullscreen mode Exit fullscreen mode

For nested dictionaries, consider using the defaultdict from the collections module or libraries like dotmap or pydash.

from collections import defaultdict

nested_data = defaultdict(lambda: 'Unknown')
nested_data['name'] = 'Alice'
print(nested_data['age'])  # Outputs: Unknown
Enter fullscreen mode Exit fullscreen mode

References


3. Silent Errors with try-except Overuse

The Problem

Overusing or misusing try-except blocks can lead to silent errors, where exceptions are caught but not properly handled. This can make bugs difficult to detect and debug.

Example

try:
    result = 1 / 0
except:
    pass  # Silently ignores the error
print("Continuing execution...")
Enter fullscreen mode Exit fullscreen mode

In the above example, the ZeroDivisionError is caught and ignored, but this can mask the underlying issue.

The Solution

Always specify the exception type you are catching, and handle it appropriately. Logging the error can also help in tracking down issues.

try:
    result = 1 / 0
except ZeroDivisionError as e:
    print(f"Error: {e}")
print("Continuing execution...")
Enter fullscreen mode Exit fullscreen mode

For broader exception handling, you can use logging instead of pass:

import logging

try:
    result = 1 / 0
except Exception as e:
    logging.error(f"Unexpected error: {e}")
Enter fullscreen mode Exit fullscreen mode

References


4. Integer Division: The Trap of Truncation

The Problem

Before Python 3, the division of two integers performed floor division by default, truncating the result to an integer. Although Python 3 resolved this with true division (/), some developers still face issues when unintentionally using floor division (//).

Example

print(5 / 2)  # Outputs: 2.5 in Python 3, but would be 2 in Python 2
print(5 // 2)  # Outputs: 2
Enter fullscreen mode Exit fullscreen mode

The Solution

Always use / for division unless you specifically need floor division. Be cautious when porting code from Python 2 to Python 3.

print(5 / 2)  # Outputs: 2.5
print(5 // 2)  # Outputs: 2
Enter fullscreen mode Exit fullscreen mode

For clear and predictable code, consider using decimal.Decimal for more accurate arithmetic operations, especially in financial calculations.

from decimal import Decimal

print(Decimal('5') / Decimal('2'))  # Outputs: 2.5
Enter fullscreen mode Exit fullscreen mode

References


5. Memory Leaks with Circular References

The Problem

Python's garbage collector handles most memory management, but circular references can cause memory leaks if not handled correctly. When two or more objects reference each other, they may never be garbage collected, leading to increased memory usage.

Example

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

node1 = Node(1)
node2 = Node(2)
node1.next = node2
node2.next = node1  # Circular reference

del node1
del node2  # Memory not freed due to circular reference
Enter fullscreen mode Exit fullscreen mode

The Solution

To avoid circular references, consider using weak references via the weakref module, which allows references to be garbage collected when no strong references exist.

import weakref

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

node1 = Node(1)
node2 = Node(2)
node1.next = weakref.ref(node2)
node2.next = weakref.ref(node1)  # No circular reference now
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can manually break the cycle by setting references to None before deleting the objects.

node1.next = None
node2.next = None
del node1
del node2  # Memory is freed
Enter fullscreen mode Exit fullscreen mode

References


Conclusion

Even in 2024, Python developers continue to encounter these common bugs. While the language has evolved and improved over the years, these issues are often tied to fundamental aspects of how Python works. By understanding these pitfalls and applying the appropriate solutions, you can write more robust, error-free code. Happy coding!


Written by Rupesh Sharma AKA @hackyrupesh

Top comments (1)

Collapse
 
hackyrupesh profile image
Rupesh Sharma

hello guys

Some comments may only be visible to logged-in visitors. Sign in to view all comments.