Sitemap

Debugging Python Code Like a Senior Developer

4 min readMar 26, 2026

--

Production is on fire. Logs are streaming. Juniors are adding print()s everywhere. Meanwhile, a senior calmly opens one log, isolates the failing request, and quietly narrows in on the real cause.

What separates them isn’t syntax knowledge — it’s debugging process. Seniors don’t guess faster. They debug slower, more systematically, and get to the right answer faster.

By the end of this article, you’ll have a repeatable debugging playbook that mirrors how experienced engineers think under pressure.

Press enter or click to view image in full size

Why Seniors Debug Differently From Juniors

Common junior habits

  • Sprinkling print() statements everywhere.
  • Restarting servers after every edit.
  • Making multiple changes at once hoping one will fix it.

Senior habits

  • Start by clarifying what’s actually broken and what still works.
  • Narrow the surface area before touching any code.
  • Treat debugging as an experiment loop — each action tests a hypothesis.

Seniors debug with discipline. They slow down before acting.

Seniors are methodical, not magical. They debug with a process, not intuition alone.

Slowing Down First: Reproducing and Framing the Bug

You can’t fix what you can’t see twice.

Step one is reproducibility — make the bug happen on demand. That often means building a smaller or controlled version of the failure.

Steps to reproduce

  1. Capture the exact inputs, configs, and environment where it fails.
  2. Recreate that setup locally or in staging.
  3. Write a failing test that represents the bug.

Example:

def test_handles_empty_email():
user = create_user(email="")
result = send_welcome_email(user)
assert result.status == "ok" # fails!

Now you can run this test repeatedly without guessing.

Quick bug note template

  • Observed: sending empty emails crashes.
  • Expected: should skip or log a warning.
  • Environment: staging, Python 3.11, celery worker.
  • Steps: run send_welcome_email("").

If you cannot reliably reproduce a bug, you cannot reliably prove you fixed it.

Using the Right Tools: Logs, Breakpoints, and Traces

Senior devs don’t rely on prints — they use visibility tools intentionally.

Logging wisely

Add context, not noise:

import logging
logger = logging.getLogger(__name__)

logger.info("Sending email", extra={"user_id": user.id, "template": tpl.name})

Good logs tell who, what, and why — not just “reached here”.

Using the debugger

def send_email(user):
breakpoint() # or import pdb; pdb.set_trace()
smtp.send(...)

Step through to inspect variable states instead of guessing.

Reading stack traces

Don’t panic at the wall of text. Start from the last call in your code, not in the library.

Example snippet:

Traceback (most recent call last):
File "service.py", line 42, in process_user
send_email(user)
File "mailer.py", line 15, in send_email
smtp.connect()
ConnectionError: timeout

Jump directly to mailer.py:15. That’s where the real story starts.

Choose the smallest tool that gives you visibility where the code is actually going wrong.

Reasoning From Symptoms to Root Cause

Debugging is detective work. Move from visible symptoms to invisible causes by shrinking the search space.

Get Code With Ahmad’s stories in your inbox

Join Medium for free to get updates from this writer.

The senior workflow

  1. Start from the symptom — e.g., “emails not sending.”
  2. Walk backward through the data or call flow.
  3. Form one hypothesis at a time (“SMTP config broken?”, “timeout too low?”, “wrong user data?”).
  4. Run one small experiment to rule it out.

Example

Your service reports “Invalid timestamp” errors.
Logs show timestamps like "2026-01-20T00:00:00Z". Looks fine — until you realize the system runs in UTC+5.

Root cause: a timezone assumption deep in the serializer. The senior didn’t guess it; they followed the data backward until the mismatch appeared.

Every debugging step should shrink the search space until only the real cause is left.

Debugging in Production Without Making Things Worse

Debugging locally is easy. Debugging in production requires restraint.

Senior habits in production

  • Add temporary, targeted logs — not random print statements.
  • Roll out changes to one node or user before all.
  • Use feature flags or env toggles to enable diagnostics safely.

Example diagnostic log:

logger.warning("Email retry failed",
extra={"user_id": uid, "attempt": n, "exception": str(e)})

Communicate clearly

  • Keep teammates informed of what you’re testing.
  • Write a short incident note — timestamped, with hypotheses tested and results.
  • Avoid panic merges; a calm patch beats a rushed fix.

In production, a calm, minimal change that improves observability is often the best first move.

Building Debuggable Code Before Bugs Happen

The best debugging is prevention — or at least preparation.

Patterns that make debugging easier

  • Structured logging with IDs, parameters, and context.
  • Assertions and invariants that fail loudly when assumptions break.
  • Type hints and result objects to catch silent None returns.

Example:

def process_order(order: Order) -> Result:
assert order.items, "Empty order"
...
return Result.ok()

Small proactive habits

  • Keep functions pure where possible.
  • Log failures once, not in every layer.
  • Centralize error handling instead of scattering try/except everywhere.

Seniors write code today that they know they will have to debug a year from now.

Turning One Fix Into a Long Term Improvement

The bug is gone — now make sure it stays gone.

After every fix, ask:

  1. Why did this happen?
  2. Why wasn’t it caught sooner?
  3. What small change would prevent similar bugs?

Add missing tests, improve logs, or simplify the fragile area. Then document what you learned.

A tiny example postmortem note:

Root cause: null config caused default timeout = 0
Fix: set default to 30s, add test
Follow-up: add startup check for missing configs

Keep these notes in a shared space — they’re gold for onboarding and prevention.

Seniors treat each bug as a chance to upgrade the system and their future debugging speed.

Debugging like a senior isn’t about speed — it’s about control. The paradox: the slower you start, the faster you finish.

So, what kind of bug rattles you most — race conditions, data mismatches, or something else? And what one change could make your next debugging session calmer?

--

--

Code With Ahmad
Code With Ahmad

Full-stack engineer with 7+ years in Python, Django, React, and Next.js. Delivered 250+ projects for 100+ clients, from APIs to full web apps.