471,317 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

pprinting objects

Hi,
Is there a way to get a dump of the insides of an object? I thought pprint
would do it. If I had a class like this:

class t:
def __init__(self):
self.x=1
self.y=2
self.obj = SomeOtherObj()

Then it could display it as:

<classt,
x,1,
y,2,
obj,<class SomeOtherObj>

Or something like that -- a complete output of the object really, with
id()'s and so forth.
\d

Dec 8 '07 #1
4 952
On Dec 8, 9:16 pm, Donn Ingle <donn.in...@gmail.comwrote:
Hi,
Is there a way to get a dump of the insides of an object? I thought pprint
would do it. If I had a class like this:

class t:
def __init__(self):
self.x=1
self.y=2
self.obj = SomeOtherObj()

Then it could display it as:

<classt,
x,1,
y,2,
obj,<class SomeOtherObj>

Or something like that -- a complete output of the object really, with
id()'s and so forth.

\d
AFAIK you have to roll your own. Here is a very rudimentary example:

C:\junk>type dumpobj.py
class MixDump(object):
def dump(self):
print "Dump of", self.__class__.__name__, 'instance'
for attr, value in sorted(self.__dict__.iteritems()):
print attr, repr(value)

class T(MixDump):
def __init__(self):
self.x=1
self.y=2
self.obj = list()
def amethod(self):
pass

class U(MixDump):
def __init__(self):
self.f = 'foo'
self.yorick = 'y'
self.bananas = None

t = T()
t.dump()
u = U()
u.dump()

C:\junk>dumpobj.py
Dump of T instance
obj []
x 1
y 2
Dump of U instance
bananas None
f 'foo'
yorick 'y'

HTH,
John

Dec 8 '07 #2
AFAIK you have to roll your own. Here is a very rudimentary example:
Very cool, thanks.

\d
Dec 8 '07 #3
"Donn Ingle" schrieb
Is there a way to get a dump of the insides of an object?
I thought pprint would do it.
print would actually like to do it if you told it how to do it.
print actually does it, but takes a default implementation if
you do not override __repr__ or __str__.
If I had a class like this:

class t:
def __init__(self):
self.x=1
self.y=2
self.obj = SomeOtherObj()

Then it could display it as:

<classt,
x,1,
y,2,
obj,<class SomeOtherObj>

Or something like that -- a complete output of the object really,
with id()'s and so forth.
Define a __repr__ or __str__ method for the class:

class t:
def __init__(self):
self.x=1
self.y=2
self.obj = SomeOtherObj()

def __repr__(self):
s = "<class%s\n x,%d\n y,%d\n obj,<class %s>" \
% (self.__class__, self.x, self.y, self.obj.__class__)
return s

a_t = t()
print "a t obj: %s" % (a_t)
a t obj: <class__main__.t
x,1
y,2
obj,<class __main__.SomeOtherObj>
HTH
Martin
Dec 8 '07 #4
Define a __repr__ or __str__ method for the class
Yes, then I could include the code John Machin suggested in there:

for attr, value in sorted(self.__dict__.iteritems()): blah

That will do nicely. Thanks all.

\d
Dec 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by dasod | last post: by
11 posts views Thread by thechaosengine | last post: by
9 posts views Thread by Aguilar, James | last post: by
161 posts views Thread by KraftDiner | last post: by
21 posts views Thread by George Exarchakos | last post: by
27 posts views Thread by SasQ | last post: by
14 posts views Thread by Jess | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.