Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What should you absolutely never do when using Python?
Python is a powerful and beginner-friendly programming language, but there are several critical mistakes that can lead to bugs, security issues, or poor performance. Here are the most important things you should absolutely never do when using Python.
Never Use Mutable Default Arguments
One of the most dangerous mistakes in Python is using mutable objects (like lists or dictionaries) as default function arguments ?
# WRONG: Mutable default argument
def add_item(item, items=[]):
items.append(item)
return items
# This creates unexpected behavior
print(add_item("apple"))
print(add_item("banana")) # Contains both apple and banana!
['apple'] ['apple', 'banana']
Instead, use None as the default and create the mutable object inside the function ?
# CORRECT: Use None as default
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
print(add_item("apple"))
print(add_item("banana")) # Fresh list each time
['apple'] ['banana']
Never Ignore Proper Indentation
Python uses indentation to define code blocks. Inconsistent indentation leads to IndentationError or unexpected behavior. Always use 4 spaces per indentation level ?
# WRONG: Mixed indentation
def check_number(x):
if x > 0:
print("Positive") # 2 spaces
print("Greater than zero") # 4 spaces - IndentationError!
Never Modify a List While Iterating Over It
Modifying a list during iteration can cause items to be skipped or IndexError exceptions ?
# WRONG: Modifying list during iteration
numbers = [1, 2, 3, 4, 5, 6]
for i, num in enumerate(numbers):
if num % 2 == 0:
numbers.pop(i) # This can cause issues
print(numbers)
[1, 3, 5]
Instead, iterate over a copy or use list comprehension ?
# CORRECT: Use list comprehension numbers = [1, 2, 3, 4, 5, 6] numbers = [num for num in numbers if num % 2 != 0] print(numbers)
[1, 3, 5]
Never Ignore Case Sensitivity
Python is case-sensitive, meaning variable and Variable are different identifiers ?
count = 10 print(Count) # NameError: name 'Count' is not defined
Never Use "list" or Built-in Names as Variable Names
Using built-in function names as variables shadows the original function and can break your code ?
# WRONG: Shadowing built-in function list = [1, 2, 3] # Now 'list()' function is unusable # data = list(range(5)) # This would fail # CORRECT: Use descriptive names numbers = [1, 2, 3] data = list(range(5)) print(data)
[0, 1, 2, 3, 4]
Never Create Circular Imports
Circular imports occur when two modules import each other, leading to ImportError or AttributeError. Always design your module structure to avoid circular dependencies.
Never Use Bare Except Clauses
Using except: without specifying exception types can hide critical errors and make debugging difficult ?
# WRONG: Catches all exceptions
try:
result = 10 / 0
except: # Too broad
print("Something went wrong")
# CORRECT: Catch specific exceptions
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid value")
Cannot divide by zero
Never Return Values from __init__
The __init__ method should never return anything other than None. Attempting to return a value raises a TypeError.
Comparison Table: Good vs Bad Practices
| Bad Practice | Good Practice | Risk |
|---|---|---|
| Mutable default arguments | Use None as default |
Shared state between calls |
| Mixed indentation | Consistent 4 spaces | Syntax errors |
Bare except:
|
Specific exception types | Hidden critical errors |
| Modifying list during iteration | List comprehension or copy | Skipped items or crashes |
Conclusion
Avoiding these common Python pitfalls will make your code more reliable, readable, and maintainable. Always use consistent indentation, specific exception handling, and descriptive variable names to write professional Python code.
