Python hisses – 1

I just started learning Python, and came across two handy functions.

We want to know to a basic thing about an object. What are its attributesĀ and methods?

The answer is dir(object). It will return all the attributes and methods, including the inherited ones.

To dig deeper, what if we want to know what arguments a function takes in? The function might be related to the object we just inspected. To get the arguments do the following:

import inspect

def func(arg1, arg2, arg3=5):
pass

inspect.getargspec(func)
(['arg1', 'arg2', 'arg3'], None, None, (5,))

Hope this helps you sometime!

Advertisements