Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Python Fundamentals

Lesson 55 of 55 · Python

Best‑Practices & Debugging

Duration: 15 minutes

Best‑Practices & Debugging

Even after you know how to write code, writing good code and fast debugging are what make a professional developer.

1️⃣ Code‑style & Linting

  • PEP 8 is the official style guide. Enforce it with flake8.
  • Black automatically formats code (black .).
  • isort sorts imports (isort .).
pip install flake8 black isort
flake8 src/   # shows style warnings
black src/    # rewrites files in‑place
isort src/   # tidy import order

2️⃣ Debugging with pdb

Insert breakpoint() (Python 3.7+) anywhere:

def compute(x):
    breakpoint()   # drops you into the REPL
    return x * 2

You can inspect variables, step (n), continue (c) or quit (q).

For a richer experience install ipdb:

pip install ipdb

Replace breakpoint() with import ipdb; ipdb.set_trace().

3️⃣ Profiling & Performance

  • cProfile gives you a call‑graph of where time is spent.
python -m cProfile -s cumulative myscript.py
  • For line‑by‑line profiling use line_profiler.
pip install line_profiler
kernprof -l -v myscript.py

4️⃣ Logging vs. print

import logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

logging.debug('Debug details')
logging.info('Normal operation')
logging.warning('Something odd')
logging.error('Error occurred')

You can switch the level without editing code, pipe logs to a file, and add rotating file handlers for production.

5️⃣ Checklist for Production‑Ready Code

  • PEP 8 compliance (flake8/black).
  • Typed where possible (mypy --strict).
  • Unit tests with ≥80 % coverage (pytest --cov).
  • Logging set to INFO+, with optional DEBUG flag.
  • No hard‑coded secrets – use environment variables or .env files (python‑dotenv).
  • CI (GitHub Actions, GitLab CI) runs lint, tests and type‑check on every PR.

Following this checklist will keep your code clean, maintainable, and easier to debug when things go wrong.

Previous: Testing with pytestBack to Python Fundamentals