How do you use application debugging and introspection (inspect, IDE debuggers)?
Answer
IDE debuggers
- Use breakpoints, step over/into/out, and inspect variables in the current frame. VS Code and PyCharm support Python debugging; attach to a process or run with “Debug” to hit breakpoints. For web apps, run the server in debug mode and trigger requests; the debugger stops in your route or service code. Use conditional breakpoints or logpoints when you need to stop only for certain inputs.
inspect module
inspect.getsource(obj)/inspect.getsourcefile(obj)— get source code or file of a function/class.inspect.signature(func)— get parameter names and annotations.inspect.getmro(cls)— get method resolution order for inheritance.inspect.ismodule(),inspect.isfunction(),inspect.iscoroutine()— check object type.inspect.stack()/inspect.currentframe()— get call stack; useful for logging or debugging “who called me.”inspect.getmembers(obj)— list attributes and methods; useful for exploring unknown objects or generating docs.
pdb and breakpoint()
- Insert
breakpoint()(Python 3.7+) orimport pdb; pdb.set_trace()to drop into an interactive debugger. Usen(next),s(step into),c(continue),p expr(print),l(list code),w(where/stack). Good for quick ad-hoc debugging when an IDE isn’t attached.
Logging and observability
- Add structured logging (e.g. request ID, user, duration) at key points. Use log levels (DEBUG for dev, INFO/WARNING for prod). Optionally add tracing (OpenTelemetry) so you can trace a request across services and async tasks. Correlate logs with metrics and traces for incident investigation.
Profiling
- Use
cProfileorpy-spyfor CPU profiling to find bottlenecks. Usetracemallocormemory_profilerfor memory. Useasynciodebug mode or similar for async-specific issues (blocking calls, slow callbacks). Profile in conditions close to production (load, data size).
Interview angle
- “How do you debug a production issue you can’t reproduce?” - start from telemetry: structured logs filtered by correlation ID, traces for where time went, and metrics for when it started. Reproduce locally only once you know the conditions.
- “What do you reach for locally?” -
breakpoint()for interactive inspection,py-spyfor a live process without modifying it,tracemallocfor memory growth, andcProfilebefore optimising anything. - “How do you investigate a memory leak?” -
tracemallocsnapshots compared over time, and check for the usual causes: unbounded caches, accumulating module-level state, and reference cycles holding objects with__del__.