examining python objects

R

rurpy

Is there a function/class/module/whatever I can use to
look at objects? I want something that will print the object's
value (if any) in pretty-printed form, and list all it's attributes
and their values. And do all that recursively.
I want to be able to find out everything about an object that
Python can introspectively find out.

Something like the 'x' command does in the Perl debugger.

Anything like this exist? (And why isn't something like this
already in pdb?)
 
B

Ben Finney

Is there a function/class/module/whatever I can use to look at
objects?

The repr() function is what you want.
I want something that will print the object's value (if any) in
pretty-printed form, and list all it's attributes and their values.
And do all that recursively.

The repr() function returns what the object's __repr__ method returns.
You'll notice that the builtin container types (string, set, list,
dict, ...) will show the values of their referred objects also.

Define the __repr__ method on your classes so that they show whatever
information you think is useful for debugging.
 
R

rurpy

__repr__ almost always only prints a summary of it's
object, not the detailed internal structure that I want to
see. When it prints values, that are not pretty-printed,
nor are the objects that constitute the value printed
recursively.

Writing my own __repr__() is emphatically what I don't
want to do! That is no better than debugging by inserting
print statements, a technique from the 1980's.

I am surprised (err, astounded actually) that a basic
tool like this isn't available. Besides debugging, I would
think it would be very helpful to people leaning python.

Perhaps one of the Python IDEs contains something
like this I could extract from it but I was hoping to shortcut
what will be a time consuming search.
 
C

Chris Mellon

__repr__ almost always only prints a summary of it's
object, not the detailed internal structure that I want to
see. When it prints values, that are not pretty-printed,
nor are the objects that constitute the value printed
recursively.

Writing my own __repr__() is emphatically what I don't
want to do! That is no better than debugging by inserting
print statements, a technique from the 1980's.

It's still a good idea, though
I am surprised (err, astounded actually) that a basic
tool like this isn't available. Besides debugging, I would
think it would be very helpful to people leaning python.

All of this functionality is intrinsically available within Python -
it's a dynamic language and you can easily inspect an object directly
to see what it looks like. Try (pretty) printing the objects __dict__
or __slots__.
Perhaps one of the Python IDEs contains something
like this I could extract from it but I was hoping to shortcut
what will be a time consuming search.

wxPython includes a graphical shell & namespace browser which you may
find useful.
 
R

rurpy

-- snip --

It's still a good idea, though

Yes and I often resort to it when I have no other choice.
But it is often faster and more efficient to step through
a section of problem code in pdb and look at variables.
(And no I don't want to use a hevyweight or gui based ide.)
All of this functionality is intrinsically available within Python -
it's a dynamic language and you can easily inspect an object directly
to see what it looks like. Try (pretty) printing the objects __dict__
or __slots__.

Well, my reason for posting is that i've spent a couple days
already trying to roll my own and it is not so easy. For one
thing, my knowlage about all of the nitty gritty of the internals
of objects is limited. A big part of my wanting this is to help
me understand objects better. Does __dict__ and __slots__
give you all the attributes of an object? I thought new-style
objects didn't even have a __dict__. I notice dir() does not
list __dict__ as an attribute, even though it is one. You can
see my understanding of these things is rather limited right
now (due in no small part to Python's shitty docs.)

While doing this is a educational, I am (supposed to be) working
on something else I need to finish. I think it is more efficient to
learn by looking at objects with a pre-written tool, then to spend
a week or more trying to write it myself. And also, I also don't
like taking days to duplicate something I'm sure has already been
done numerous times.
wxPython includes a graphical shell & namespace browser which you may
find useful.
-- snip --

I'll look for this but I'd rather find something I didn't have to hack.
What a bummer, maybe I should be posting this to the "reinventing
the wheel" thread. :-(
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Is there a function/class/module/whatever I can use to
look at objects? I want something that will print the object's
value (if any) in pretty-printed form, and list all it's attributes
and their values. And do all that recursively.
I want to be able to find out everything about an object that
Python can introspectively find out.

Then check the inspect module
 
R

rurpy

Bruno said:
(e-mail address removed) a écrit :

Then check the inspect module

I want a callable, ready-to-use class or function.
Inspect provides soime funtions that would be useful for wrinting
such a class or function, but does not provide one.

I seems that nobody who has written or used such a tool reads
this group, or feels like responding.

FWIW, (for anyone looking for something similar in the future)
I found http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/137951
which will format and print an object's attributes. By combining that
and pprint in the Python distrib, I think can coble up what I am
looking
for.

Still, it is discouraging that such a basic thing is not provided with
python, or at lleast easily available in some library.
 
R

rurpy

Bruno said:
(e-mail address removed) a écrit :

Then check the inspect module

I want a callable, ready-to-use class or function.
Inspect provides soime funtions that would be useful for wrinting
such a class or function, but does not provide one.

I seems that nobody who has written or used such a tool reads
this group, or feels like responding.

FWIW, (for anyone looking for something similar in the future)
I found http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/137951
which will format and print an object's attributes. By combining that
and pprint in the Python distrib, I think can coble up what I am
looking
for.

Still, it is discouraging that such a basic thing is not provided with
python, or at lleast easily available in some library.
 
C

Colin J. Williams

I want a callable, ready-to-use class or function.
Inspect provides soime funtions that would be useful for wrinting
such a class or function, but does not provide one.

I seems that nobody who has written or used such a tool reads
this group, or feels like responding.

FWIW, (for anyone looking for something similar in the future)
I found http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/137951
which will format and print an object's attributes. By combining that
and pprint in the Python distrib, I think can coble up what I am
looking
for.

Still, it is discouraging that such a basic thing is not provided with
python, or at lleast easily available in some library.
In the interactive mode, you might try >> help(object)

Colin W.
 
R

rurpy

Colin said:
In the interactive mode, you might try >> help(object)

I want all the information about the object I can get through
introspection. help() give some (a lot more than I realized,
so thanks) but not everything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top