Python

Python Tips and Tricks

Printing Arbitrary Objects

Sample arbitrary object:

response = requests.request(method='GET', url='https://asdf.com')

YAML:

import yaml # pip3 install yaml print(yaml.dump(response))

JSON:

import jsonpickle # pip3 install jsonpickle print(jsonpickle.encode(response, indent=2))

Via pprint:

import pprint pprint.pprint(vars(response))

Other options:

    • Consider the inspect module (via import inspect) for a few more options

    • dir(response) to get a list of the available names of functions and properties

    • help(response) to get a man-page

[Edit]