473,568 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 996
On Dec 8, 9:16 pm, Donn Ingle <donn.in...@gma il.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.__d ict__.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.__clas s__)
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__.SomeOt herObj>
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.__d ict__.iteritems ()): blah

That will do nicely. Thanks all.

\d
Dec 8 '07 #5

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

Similar topics

2
8240
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list iterators and how they are behaving in situations like this. #include <iostream> #include <list> class Test; typedef std::list< Test* > Tlist;
11
1762
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets take an example of user management against a database. Things I might like to do include:
9
1871
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather...
6
2560
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically,...
3
3018
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements - also allow them? Adding (and removing) object properties dynamically is an acceptable and common practice in JavaScript, and greatly adds to the...
161
7752
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
7
8208
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
21
2192
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not necessarily the same... class base { int x;
27
2530
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the Standard is saying that "object is a region of storage", I deduce from that that literal constants aren't objects because they may not be alocated...
14
5989
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.