Objprint - Pretty Print Python Objects
objprint is a Python library designed to print Python objects in a human-readable format, simplifying debugging and inspection. It provides a primary function `op()` and a decorator `@add_objprint` for custom classes. The library is actively maintained, with its current version being 0.3.0, and has a fairly regular release cadence addressing new Python versions and adding features.
Warnings
- breaking The primary printing function was renamed from `objprint` to `op` in version 0.1.2. Code using `objprint(my_object)` will break.
- gotcha Objprint includes recursion handling to prevent infinite loops when printing circularly referenced objects. This means deeply nested or self-referential structures might not be fully expanded by default.
- gotcha Prior to version 0.2.2, objprint used `__dict__` to retrieve attributes, which might not show all attributes for objects that don't primarily store state in `__dict__` (e.g., objects using `__slots__` or C-extensions).
- gotcha The `print_methods` feature (introduced in 0.2.0) can significantly increase output verbosity by including callable methods. This is off by default, but if enabled, can make output overwhelming.
Install
-
pip install objprint
Imports
- op
from objprint import op
- add_objprint
from objprint import add_objprint
- config
from objprint import config
Quickstart
from objprint import op, add_objprint
class MyObject:
def __init__(self, name, value):
self.name = name
self.value = value
self.items = [1, {'a': 2}, 'hello']
def get_info(self):
return f"{self.name}: {self.value}"
@add_objprint
class AnotherObject:
def __init__(self, id_val):
self.id = id_val
self.nested_obj = MyObject("Nested", 99)
# Print a standard object
my_instance = MyObject("Test", 42)
op(my_instance)
# Print an object decorated with @add_objprint
another_instance = AnotherObject(123)
op(another_instance)
# Example of global configuration (optional)
# from objprint import config
# config(indent=4, color=False)
# op(my_instance)