tags:

views:

101

answers:

3

I just want to quickly see the properties and values of an object in Python, how do I do that in the terminal on a mac (very basic stuff, never used python)?

Specifically, I want to see what message.attachments are in this Google App Engine MailHandler example (images, videos, docs, etc.).

+3  A: 

Update

There are better ways to do this than dir. See other answers.

Original Answer

Use the built in function dir(fp) to see the attributes of fp.

Manoj Govindan
great thank you, now I can just loop through those and print `key = object[value]` (key = value). is there a better way?
viatropos
I cannot think of another way expect perhaps `getattr`, another built in function.
Manoj Govindan
@Manoj Govindan. See the `inspect` module.
aaronasterling
There are better ways, but if you're just poking around in the console, trying to get an idea of what options are available on a particular object/module, `dir` is a nice quick, simple one-liner that doesn't require any imports or anything.
MatrixFrog
+4  A: 

use the getmembers attribute of the inspect module

It will return a list of (key, value) tuples. It gets the value from obj.__dict__ if available and uses getattr if the the there is no corresponding entry in obj.__dict__. It can save you from writing a few lines of code for this purpose.

aaronasterling
+4  A: 

If you want to dump the entire object, you can use the pprint module to get a pretty-printed version of it.

from pprint import pprint

pprint(my_object)

# If there are many levels of recursion, and you don't want to see them all
# you can use the depth parameter to limit how many levels it goes down
pprint(my_object, depth=2)

Edit: I may have misread what you meant by 'object' - if you're wanting to look at class instances, as opposed to basic data structures like dicts, you may want to look at the inspect module instead.

Amber