Junior — Middle
What methods and tools do you use to find and fix bugs in code?
sobes.tech AI
Answer from AI
To find and fix errors in Python code, I use several methods and tools:
- Logging: I add logging using the
loggingmodule to track the execution flow and variable values. - Debugger: I use the built-in
pdbor IDE debuggers (such as in PyCharm or VSCode) for step-by-step execution, viewing call stacks, and variable values. - Unit tests: I write tests using
unittestorpytestto identify errors early. - Static code analysis: I employ tools like
flake8,pylint, ormypyto find potential issues and type mismatches. - Interactive work: I run problematic sections in an interactive console or Jupyter Notebook for quick hypothesis testing.
Example of using pdb:
import pdb
def faulty_function(x):
pdb.set_trace() # breakpoint
return 10 / x
faulty_function(0)
When executed, the program will stop at set_trace(), allowing you to check values and step through the code.