473,915 Members | 7,703 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 1005
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
8263
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
1806
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
1899
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 than references for the STL library? I'd also prefer to avoid using const_cast, if it is indeed...
6
2595
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, I want to store classes of my objects in a vector. I also have three further questions:
3
3044
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 power and character of the language. Essentially, an object in JavaScript can be considered to...
161
7965
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
8231
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
2233
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
2580
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 as regions of storage in the memory.
14
6048
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
9883
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11359
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11069
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10543
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9734
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7259
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3370
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.