What is a Python equivalent of PHP's var_dump()?

What is a Python equivalent of PHP's var_dump()?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

What is a Python equivalent of PHP's var_dump()?

In PHP, var_dump() is one of those functions you reach for instinctively when something goes wrong or you need to understand the state of your application quickly. It prints out the type of every variable, its length, and its value in a single go. It’s raw, unfiltered, and often exactly what you need during the debugging phase.

Python doesn't have a built-in function with the exact same name or behavior baked into the language core. Instead, the ecosystem has converged on a few distinct tools depending on your specific needs: the standard library's pprint, the third-party dump package, and the ubiquitous debugger console. Each serves a slightly different purpose, and understanding which one to use depends on whether you want a quick snapshot or a deep dive into object internals.

The Standard Library Approach: pprint

If your primary goal is to see a human-readable representation of complex data structures like dictionaries, lists, or custom objects, the standard library offers pprint. Unlike PHP's var_dump(), which outputs everything in one continuous stream, pprint formats output with indentation and line breaks. This makes it significantly easier to read large structures without manually scrolling through a terminal window.

You can use it simply by importing the module:

from pprint import pprint

data = {
    "user": {"name": "Alice", "roles": ["admin", "editor"]},
    "settings": {
        "theme": "dark",
        "notifications": True
    }
}

pprint(data)

This will output a nicely formatted block, making it obvious how nested keys relate to one another. A key distinction here is that pprint defaults to limiting the width of lines (usually 80 characters), wrapping long strings or values if necessary. This behavior can be useful for terminal readability but sometimes obscures very long strings compared to PHP's raw output.

The Direct Equivalent: The dump() Package

If you are looking for a drop-in replacement that behaves almost exactly like PHP's var_dump(), the third-party package named dump is the closest match. You can install it via pip (pip install dump) and import it as a function.

from dump import dump

data = {"status": "error", "code": 500}
dump(data)

This outputs the type, length, and value in a single line per variable, mimicking the classic PHP style. It is particularly handy when you want to inspect an object without importing additional formatting logic or dealing with indentation preferences. However, be aware that this is external code; relying on it means depending on a third-party package rather than using standard library features.

The Professional Choice: Interactive Debugging

While printing to stdout works for quick checks, experienced Python developers often prefer stepping away from print-based debugging entirely. Tools like the built-in debugger (pdb) or modern IDE consoles allow you to pause execution and inspect variables interactively.

Instead of cluttering your code with dump calls that flood the output log, you can set a breakpoint:

import pdb; pdb.set_trace()

Once paused, you can type data into the console to see its representation, just as you would in a PHP environment where var_dump() is implicit. This approach is cleaner because it keeps your script logic separate from your investigative process. It also allows for deeper inspection, such as calling methods on an object or navigating the call stack.

Trade-offs and Considerations

The main trade-off between these approaches is verbosity versus interactivity. Using pprint or a dump() package adds lines to your code every time you need to inspect a variable. If you find yourself inserting multiple dump calls throughout a script, it suggests that the issue might be better solved by adding a proper logging statement or using an interactive debugger session.

Additionally, PHP's var_dump() is often paired with error reporting settings to automatically trigger on specific exceptions. In Python, this functionality is handled differently through exception handlers and loggers rather than a single function call. Understanding these architectural differences helps avoid the habit of overusing dump functions in production code where output volume can become problematic.

When debugging complex applications, especially those running in production environments via WSGI servers like Gunicorn or Uvicorn, dumping large objects to stdout can exhaust file descriptors or slow down request processing. In such cases, leveraging an interactive session or writing debug info to a dedicated log file is often the safer architectural choice.

Our network