← Home
⁉️

Zen of Debugging in Python - Part 1

Knowing when to use which tool for logging info into the console can make huge difference. This blog is about revisiting that

Examples are notes; the sandbox is right here when you want to poke at browser-safe Python.

Try it here

Python scratchpad

Full sandbox

Favor logging for detailed analysis; fallback to pprint for clarity, and print for simplicity.

← this item is applicable for step 1 of debugging

← this item is applicable for step 1 of debugging

Let’s break down the scenarios and discuss the appropriate usage of print statements, pprint, and logging.

Using print Statements

Print statements are straightforward to use. You can simply use the print() function to output messages to the console.

print("My awesome blog!")

They are useful for -

  • quick debugging and understanding the flow of the program.
  • providing immediate feedback during development.

They can be preferred -

  • For small scripts or quick debugging purposes.
  • When all we need is simple, ad-hoc logging without much formatting.

Should not be preferred -

  • In large-scale applications where you need more structured logging and better control over log levels.
  • In production code where you need to log errors, warnings, and informational messages systematically.
  • When you need more standardization and customization options in a broader way

Using pprint instead of print

pprint is particularly useful when you want to print complex data structures like dictionaries or lists in a more readable format.

To use pprint though, we need to import the pprint module and use pprint() function to print the data structures.

from pprint import pprint

# Example usage:
pprint(my_complex_data_structure)

The prime reasons as to why we would prefer pprint over print is

  • Provides more readable output for complex data structures.
  • Helpful in understanding nested structures at a glance.

However we don’t need to use pprint if we have something easy to print, like a simple string or a value that can be represented in a string. Also incases

Using logging instead of print or pprint

logging is a more robust and flexible way of handling logging compared to print statements.

While print statements are useful for basic debugging, logging provides a more robust solution for detailed analysis. Python’s built-in logging module allows you to log messages at various levels (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL), enabling you to control the verbosity of your logs.

It can be preferred for the below reasons -

  • Provides better control over log levels (debug, info, warning, error, etc.).
  • Enables logging to various destinations (file, console, etc.).
  • Allows customization of log message formatting.
  • In large-scale applications or production code where structured logging is essential.
  • When you need to log messages with different log levels.

If we want to use logging here is an example -

  • Import the logging module and configure loggers, handlers, and formatters as needed. Then use logging.info(), logging.error(), etc., to log messages.
import logging

# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Example usage:
logging.info("This is an informational message")

The only cons in this however can be that - it requires additional setup and configuration compared to print, and that it may add complexity to simple scripts.

Conclusion:

Choose the appropriate method based on the requirements of your project, considering factors like simplicity, readability, and scalability.