Exploring Python Objects

Exploring Python Objects

When I started with Python, it was not the most elegant start that someone would have wanted in their tech career. It was more of a forced move as work required me to code in Python so I eventually used it. But I never got any chance to learn it properly. And just as today, most of my learning was based on searching solutions on StackOverflow. The only difference is, today, before searching for any solution on the web, I try to explore any object that I am working with and figure out what other functions and properties it has to offer.

How do I do it is what this article is going to be. So let's kick off the article with an example.

Exploring the PyGithub library

Lately, I have been working with PyGithub which enables you to perform a majority of the GitHub operation through Python Code. We begin with installing the library.

pip install pygithub

Here's a quick tip: Make sure to create a separate virtual environment before testing out any library as it may have a dependency on another library and can cause version conflict.

Once installed, we import the Github class from GitHub package

from github import Github

At this point reading PyGithub's Documentation would be a good idea and I would encourage you to go through it.

Moving on, now that we have a Github class with us, how do we know what functions variables and class variables it has to offer. Here's is the first way to do it.

1. help() method

When you have any python object whose structure you would like to know, pass that object as an argument to the help() function. In our example, we'll do that for Github class

help(Github)

This would return the class docstring, class variables and function variables the class has. Here's what output would look like:

image.png

2. dir() method

Once you are familiar with the behaviour and docstring of the various objects of the class, the dir() method would come in handy when you are a little familiar with the class and would just like to know the list of function variables and magic methods of the class. Similar to the help method, pass that object as an argument to the dir() function.

dir(Github)

This would return a list of all the magic methods, properties and functions present in the class. Here's what output would look like:

image.png

3. inspect library

This method is more suited for seasoned devs when they have been using objects for some time but would like to get a deeper look into what that object is actually doing behind the scenes.

Now that this is another library, I would encourage you to use explore it using the above two methods.

And for those who would like to continue with the flow of the article, you should find yourself using the getsource() function to get the source code of the particular object. Github class has a function called get_repo(), that would return a repository object of any GitHub repository name which is passed to it as an argument. Here's how to do it:

from github import Github
import inspect

print(inspect.getsource(Github.get_repo))

The output would contain the function source code with a docstring and parameter list.

image.png

These were the 3 techniques that I use very often in my daily Python work. If there are any other cool techniques that you might know and would like to share, drop them in the comment.