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):

(['arg1', 'arg2', 'arg3'], None, None, (5,))

Hope this helps you sometime!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s