Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "see" the structure of an object in python

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!

like image 512
jeffery_the_wind Avatar asked Aug 24 '12 12:08

jeffery_the_wind


People also ask

How do you find the data structure in Python?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

How do I see all the properties of an object in Python?

Use Python's vars() to Print an Object's Attributes The dir() function, as shown above, prints all of the attributes of a Python object.

How do you print object details in Python?

To print the attributes of an object we can use “object. __dict__” and it return a dictionary of all names and attributes of object. After writing the above code (python print object attributes), once you will print “x. __dict__” then the output will appear.

What is dir () in Python?

Python dir() Function The dir() function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are default for all object.


2 Answers

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.

like image 157
mgilson Avatar answered Oct 18 '22 10:10

mgilson


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()).

like image 45
zigg Avatar answered Oct 18 '22 09:10

zigg