5

I am wokring in python 2.7. I have been experimenting with the tweepy package. There is an object called the tweepy.models.status object, whose function is defined here: https://github.com/tweepy/tweepy/blob/master/tweepy/models.py.

I have a function that looks like this:

 def on_status(self, status): try: print status return True except Exception, e: print >> sys.stderr, 'Encountered Exception:', e pass 

The object I am referring to is the one returned from the on_status function, called status. When the print status line executes i get this printed on the screen;

tweepy.models.Status object at 0x85d32ec>

My question is actually pretty generic. I want to know how to visually print out the contents of this status object? I want to know what information is available inside this object.

I tried the for i, v in status : approach, but it says this objects is not iterable. Also not all of the object attributes are described in the function definition.

Thanks a lot!

1
  • 1
    +1'd and favourited. I've generally just used dir but now the answer from mgilson gives me something more concrete to re-use. Wanted to add: Dive Into Python (for v2, first one) has a chapter on introspection with an apihelper->info function. Also gives some useful info about members, etc. and documentation from the doc strings. Commented Aug 24, 2012 at 12:53

2 Answers 2

11

You could iterate over status.__dict__.items():

for k,v in status.__dict__.items(): #same thing as `vars(status)` print k,v 

The above approach won't work if the class uses __slots__ and doesn't have a slot for __dict__. Classes with __slots__ are quite rare though, so it's unlikely to be a problem.

Alternatively, you could use the dir builtin with getattr:

for attr in dir(status): print attr, getattr(status,attr) 

This does work for classes with __slots__, but has some limitations if a custom __getattr__ is defined (see link, and __dict__ would suffer in the same way).

Finally, if you want really fine control over what you see (e.g. if you only want methods), you can check out some of the goodies in the inspect module.

Sign up to request clarification or add additional context in comments.

4 Comments

So I tried your first suggestions and get this error: Encountered Exception: too many values to unpack ... seems like some huge data.
The second suggestion worked well! for attr in dir(status). Thanks!
@jeffery_the_wind -- Sorry about the first one. (ProgrammerError on my part). status.__dict__ is the dictionary. To iterate over key/value pairs in a dictionary you need it's items method. Updated.
@deadly -- what mistake? *whistles innocently*
6

I've always been a fan of the dir builtin for manual introspection. Works on modules, objects...

>>> import math >>> dir(math) ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] 

I believe it's equivalent to sorted(object.__dict__.keys()).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.