Dumping an instance

G

gf gf

If I want to dump (for debugging) an instance and all
of it's member attributes, what's the simplest way?

print myInstance

just gives a pointer to its location in memory

Thanks!





__________________________________
Do you Yahoo!?
Yahoo! Mail - 250MB free storage. Do more. Manage less.
http://info.mail.yahoo.com/mail_250
 
R

Roy Smith

gf gf said:
If I want to dump (for debugging) an instance and all
of it's member attributes, what's the simplest way?

print myInstance

just gives a pointer to its location in memory

You need to write a str() method for the class. Alternatively, take a look
at the pprint module.
 
P

Peter Hansen

gf said:
If I want to dump (for debugging) an instance and all
of it's member attributes, what's the simplest way?

print myInstance

just gives a pointer to its location in memory

Roughly speaking, as a starting point:

from pprint import pformat
def dump(obj):
print repr(obj)
for name in obj.__dict__:
val = getattr(obj, name)
print name, '=', pformat(val)

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,757
Latest member
PDIJaclyn

Latest Threads

Top