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 4 993
On Dec 8, 9:16 pm, Donn Ingle <donn.in...@gmail.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.__dict__.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
AFAIK you have to roll your own. Here is a very rudimentary example:
Very cool, thanks.
\d
"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.__class__)
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__.SomeOtherObj>
HTH
Martin
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.__dict__.iteritems()): blah
That will do nicely. Thanks all.
\d This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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. ...
|
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...
|
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 -...
|
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.....
|
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;
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
|
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...
| |