Good code not only works but also tells a story — it’s easy to understand, debug, and maintain. Python encourages writing self-documenting code by following clear naming conventions and adding meaningful comments where necessary. This guide introduces you to best practices for writing effective comments and using proper naming conventions in Python, as recommended by PEP 8.
1. Why Comments and Naming Matter
- Enhance code readability and maintainability
- Help future developers (and your future self) understand your code
- Make collaboration easier and more efficient
2. Commenting Best Practices
2.1 Use Comments Sparingly and Wisely
Only write comments when the code isn’t self-explanatory. Focus on explaining "why" something is done, not just "what."
# Bad
x = x + 1 # increment x by 1
# Good
# Adjusting for zero-based index
index = index + 1
2.2 Block Comments
Use block comments to explain complex logic or sections of code. They should be indented to the same level as the code they describe.
# Calculate total price including tax and discount
subtotal = price * quantity
total = subtotal + (subtotal * tax) - discount
2.3 Inline Comments
Place inline comments on the same line as the code, separated by at least two spaces.
value = compute_total() # Final value after processing all inputs
2.4 Docstrings for Functions and Classes
Use triple-quoted strings to describe the purpose and usage of functions, classes, and modules.
def greet(name):
"""Return a personalized greeting for the given name."""
return f"Hello, {name}!"
2.5 Avoid Obvious or Redundant Comments
# Bad
i = 0 # set i to 0
# Good
i = 0 # Loop counter initialization
3. Naming Conventions
Choosing the right names for variables, functions, and classes makes your code more understandable without extra comments.
3.1 Variables and Functions
- Use lowercase letters with underscores to separate words
- Names should be descriptive but concise
# Good
user_age = 25
calculate_average_score()
# Bad
x = 25
func1()
3.2 Constants
- Use ALL_CAPS with underscores for constants
PI = 3.14159
MAX_USERS = 100
3.3 Classes
- Use CamelCase for class names
class StudentProfile:
pass
3.4 Private and Internal Use
- Prefix internal or private variables/methods with a single underscore
- Prefix with double underscore to trigger name mangling (used for internal class attributes)
_internal_data = []
__secret_key = "abc123"
3.5 Modules and Packages
- Use short, all-lowercase names with optional underscores
# Good
utils.py
data_processing.py
4. Summary
- Write meaningful comments to explain "why," not just "what."
- Use docstrings for documenting functions and classes.
- Follow consistent naming conventions for variables, functions, classes, and constants.
- Use underscores and capitalization according to PEP 8 guidelines.
5. Final Thoughts
Clean code is not just about logic — it’s about communication. By writing helpful comments and using consistent naming patterns, you make your code more accessible to others and to yourself. Follow these practices consistently and your Python code will become easier to read, easier to maintain, and more professional overall.