I have 3 python classes A, B and C. A contains B object and B contains C's object. What I want is when I print A class object, it should pretty print in the below format. There can be more nesting also inside C class.
A:
loc : XYZ
qual : ABC
b :
name : ABC
age : 30
c :
address : ABC
phn : 99009
Below are the classes for reference.
class C(object):
def __init__(self):
self.address='ABC'
self.phn=99009
class B(object):
def __init__(self):
self.name='ABC'
self.age=30
self.c = C()
class A(object):
def __init__(self):
self.loc = 'XYZ'
self.qual = 'ABC'
self.b = B()
pretty-print
, I'm assuming you're just looking to control how thepprint
module handles your objects? The simplest thing to do is to create a customPrettyPrinter
subclass that overrides the methods documented there, so it handles your objects as "recursive objects" like lists and dicts. Then you just use an instance of that class in place of thepprint
module. – Byproduct