Python Comments & Shebang
Syntax Satellite
Comments & Shebang
Communicate intent to humans with comments/docstrings and to the OS with shebang lines.
Comments
- Single-line:
# explain why, not what - Block: stack single-line comments or use docstrings
- Avoid redundant comments; focus on intent, edge cases, or references.
Docstrings
def fetch_user(user_id: str) -> dict:
"""Retrieve a user profile from the API by ID."""
Use triple quotes. Tools like Sphinx or MkDocs can generate documentation from docstrings.
Shebang
#!/usr/bin/env python3
- Place at the top of executable scripts.
- Run
chmod +x script.pyand execute./script.py. - Uses
envto locate the correct interpreter on the PATH.