By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Dumping an instance

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <ma*************************************@python.or g>,
gf gf <un**************@yahoo.com> wrote:
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.
Jul 18 '05 #2

P: n/a
gf gf wrote:
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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.