Python is a high-level, interpreted programming language known for its readability and versatility. It emphasizes code readability with its notable use of significant indentation and supports multiple programming paradigms.
Python Anti-Patterns Overview
Using Mutable Default Arguments
Not Using Context Managers
with
statement) for resource management to ensure resources are properly cleaned up, even if exceptions occur.Using Wildcard Imports
Ignoring Exceptions Too Broadly
Modifying Lists During Iteration
Using type() Instead of isinstance()
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.Not Using Generator Expressions
Using Global Variables
Not Using enumerate()
enumerate()
instead of manual indexing for cleaner, more readable code.String Concatenation in Loops
join()
on a generator expression or list comprehension instead.Not Using Proper Data Structures
Reinventing the Wheel
Not Using Virtual Environments
Using Bare Except Clauses
except:
clauses catch all exceptions, including those you might not want to catch like KeyboardInterrupt
and SystemExit
. Always specify which exceptions to catch.Not Using pathlib
pathlib
for file operations. It provides an object-oriented interface and simplifies many common file operations.Not Using f-strings
Using eval() or exec()
eval()
and exec()
as they can execute arbitrary code and pose security risks. Use safer alternatives like ast.literal_eval()
when needed.Not Using Proper Logging
logging
module instead of print()
statements for debugging and monitoring. It offers more flexibility, including log levels, formatting, and output destinations.Not Using Proper Argument Parsing
argparse
module instead of manually parsing sys.argv
. It provides help messages, type conversion, and validation.Not Using Proper Testing
unittest
or pytest
instead of manual testing. They provide test discovery, assertions, setup/teardown, and reporting.