473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

easy way to dump a class instance?

Is there a way to automatically print all the instance
data in a class? This is for debugging, I would like
to do something like dump(self) to snapshot the state
of the object.

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
May 5 '06 #1
5 2017
Mark Harrison wrote:
Is there a way to automatically print all the instance
data in a class? This is for debugging, I would like
to do something like dump(self) to snapshot the state
of the object.


def dump(obj):
buf = ['%r %s :' % (obj, str(obj)]
for name in dir(obj):
attr = getattr(obj, name)
if not callable(attr):
buf.append["- %s : %s" % (name, str(attr))]
return '\n'.join(buf)

Not tested...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 5 '06 #2
Mark Harrison wrote:
Is there a way to automatically print all the instance
data in a class? This is for debugging, I would like
to do something like dump(self) to snapshot the state
of the object.

Would something like cPickle work? That allows you to dump objects into
files as they are and read them again as they were. Its fairly easy to
use and might work well for you.

HTH

-carl
--

Carl J. Van Arsdall
cv*********@mvista.com
Build and Release
MontaVista Software

May 5 '06 #3

Mark Harrison wrote:
Is there a way to automatically print all the instance
data in a class?


print self.__dict__

May 5 '06 #4
Some Googling yielded this recipe
http://aspn.activestate.com/ASPN/Coo...7951/index_txt,
a little test shows promising results of the described recipe. BTW: the
Python Cookbook is always a good place to browse with questions other
people probably had as well!

May 5 '06 #5
Kamilche a écrit :
Mark Harrison wrote:
Is there a way to automatically print all the instance
data in a class?

print self.__dict__

Yeps - if you don't mind missing class attributes and data descriptors.
May 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
3
by: Chris Stiles | last post by:
Is there a human readable dump format for objects ? As opposed to pickling ? It doesn't necessarily have to be XML - in fact i'd prefer if it wasn't ;) - but ISTR an XML dump library. --...
2
by: Jane Austine | last post by:
Hi. class A: def __init__(self,tick): if tick: self.foo=self.bar else: self.foo=self.bur def bar(self): print 'bar'
6
by: wenmang | last post by:
Here is the code which causes core dump when is built differently, 1 for regular dev built, the other for packaging build. class my_class{ public: my_class(){}; ~my_class(){}; private:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.