'Python code to print out value in any variable

What are some good ways to "pretty print" values of any variable (and anything inside the variable), whether it is:

  • scalar
  • object
  • list of scalar or objects
  • dict of scalar or objects
  • tuple of scalar or objects

Thanks



Solution 1:[1]

Building on top of Aeiddius's answer.

This works well on natives types like scalar, lists of scalars, and dicts of scalars:

from pprint import pprint

pprint(var)

But for user-defined objects, I got things like <MyClass object at 0x10fcf93d0>

Below works well on user-defined objects:

from pprint import pprint

pprint(vars(object))

There doesn't seem to be one simple solution that works for all cases on this need.

Solution 2:[2]

from pprint import pprint

pprint(var)

And if you don't like pprint, try https://pypi.org/project/pprintpp/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Aeiddius