Skip to main content
Python, despite its elegance and readability, has several common anti-patterns that can lead to bugs, performance issues, and maintenance problems. Here are the most important anti-patterns to avoid when writing Python code.
Default arguments in Python are evaluated only once at function definition time, not each time the function is called. This can lead to unexpected behavior with mutable objects like lists, dictionaries, or sets.
Always use context managers (with statement) for resource management to ensure resources are properly cleaned up, even if exceptions occur.
Wildcard imports pollute the namespace, can lead to name conflicts, and make it difficult to track where functions and classes are coming from.
Catching exceptions too broadly can hide bugs and make debugging difficult. Always catch specific exceptions and handle them appropriately.
Modifying a list while iterating over it can lead to skipped elements or index errors. Create a new list or use list comprehension instead.
Using type() doesn’t account for inheritance. isinstance() checks if an object is an instance of a class or any of its subclasses, which is usually what you want.
List comprehensions load the entire result into memory. For large datasets, use generator expressions to process items one at a time, saving memory.
Global variables make code harder to test, debug, and maintain. Use class attributes or function parameters instead.
When you need both the index and value during iteration, use enumerate() instead of manual indexing for cleaner, more readable code.
String concatenation in loops is inefficient because strings are immutable. Use join() on a generator expression or list comprehension instead.
Use appropriate data structures like namedtuples or dataclasses for better code readability and maintainability.
Python’s standard library is extensive. Before implementing functionality, check if it already exists in the standard library or a well-maintained package.
Always use virtual environments to isolate project dependencies and avoid conflicts between different projects.
Bare except: clauses catch all exceptions, including those you might not want to catch like KeyboardInterrupt and SystemExit. Always specify which exceptions to catch.
For Python 3.4+, use pathlib for file operations. It provides an object-oriented interface and simplifies many common file operations.
For Python 3.6+, use f-strings for string formatting. They are more readable, concise, and often faster than older formatting methods.
Avoid eval() and exec() as they can execute arbitrary code and pose security risks. Use safer alternatives like ast.literal_eval() when needed.
Use the logging module instead of print() statements for debugging and monitoring. It offers more flexibility, including log levels, formatting, and output destinations.
For command-line applications, use the argparse module instead of manually parsing sys.argv. It provides help messages, type conversion, and validation.
Use proper testing frameworks like unittest or pytest instead of manual testing. They provide test discovery, assertions, setup/teardown, and reporting.