Explain Codes LogoExplain Codes Logo

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

python
debugging
variable-inspection
data-detection
Anton ShumikhinbyAnton Shumikhin·Jan 7, 2025
TLDR

In Python, you get similar functionality to PHP's var_dump() using the built-in function pprint for tidy output and vars() to reveal object attributes. The pprint module tidies up complex data types such as lists and dictionaries, while vars() discloses an object's contents.

Here's a neat, clarifying example:

from pprint import pprint # Sneak peek into a dictionary treasure box my_data = {'foo': 'bar', 'numbers': [1, 2, 3]} pprint(my_data) # Gotcha! A prettified dictionary printed out class Sample: x = 10 # Meet our class instance sample_instance = Sample() pprint(vars(sample_instance)) # Whola! You've checked instances' attributes

Outputs:

{'foo': 'bar', 'numbers': [1, 2, 3]}
{'x': 10}

This approach reflects the var_dump() in displaying structured data and inspecting objects effectively.

Python's debugging jewels: Beyond var_dump

While pprint and vars() offer a palatable taste of variable inspection, Python's kitchen serves more delicious dishes for debugging. The inspect module carries the getmembers() function. Coupling it with pprint opens Pandora's Box leading to deep dive insights into live objects, their ingredients like functions, arguments, docstrings, and the like.

Consider this appetizer:

import inspect from pprint import pprint class MyClass: def __init__(self, value): self.value = value def do_something(self): pass # Say hi to our instance my_instance = MyClass(42) pprint(inspect.getmembers(my_instance, predicate=inspect.ismethod))

Serving a super-rich buffer of methods with tasty attributes of the class instance.

Python's repr() function solves the serialization puzzle like PHP's var_dump(). It curates a string that’s a valid Python expression (dog food, yum! 🐶) to recreate the original object. It satisfies the debugging sweet tooth!

Delving deeper: Advanced debugging artefacts in Python

For those who prefer scuba-diving into traceback analysis, the cgitb module is your oxygen cylinder. It's especially valuable in web development and CGI scripts and offers a super-detailed traceback report that uncovers local variables.

Enter the third-party library, var_dump, resembling PHP's var_dump() Osiris within Python's rule. Feast on it with pip:

pip install var_dump

And globally declare it in your code:

from var_dump import var_dump var_dump(your_variable) # Mimicking PHP's var_dump() here, folks!

Visualising Pythonic var_dump usage

When it comes to bringing the Python equivalent of PHP's var_dump() to life:

from pprint import pprint # Replace 'your_variable' with any variable you're probing. pprint(your_variable)

Visualising as a data detective revealing variable's details:

Your valuable vault (your_variable) 🗺️: 🔎 "Type": "list" 🔎 "Length": 4 🔎 "Contents": [gold, diamonds, rubies, emeralds]

With pprint, you'll become the Sherlock Holmes of code, uncovering all the hidden artefacts beneath a variable's surface neatly and nattily! 🕵️‍♀️

Dragons and dungeons: Exploring globals and locals

When the lighthouse of a comprehensive view of your namespace flickers, Python lits globals() and locals() lamps. These grand functions return dictionaries mirroring the global and local symbol tables.

Feast them to pprint, and you get a readable situation report of all the active variables, a godsend when debugging multi-artefact environments.

from pprint import pprint pprint(globals()) # Be global, like Sherlock's network 😉 pprint(locals()) # Pay attention to local affairs, like dear Watson 😄

With these weapons in your debug-arsenal, you are more than ready to defend the Python realms against bugs and bottlenecks.