Course Content

Object Introspection | Python Tutorials For Absolute Beginners In Hindi #70

Welcome to another tutorial guys. We are just going to open our Pycharm ide and start this tutorial without wasting any time.





We are going to name our file python tuts here. 

In today’s tutorial, we are going to learn about object introspection. We have used it a bit in our previous tutorial but never discussed it in depth. As we have discussed earlier that everything in Python is an object. All the functions we use regularly are predefined in some built-in class. For example, while printing any string, we are using the object of an str class that is predefined for the usage of string.

Object Introspection in Python

Introspection can be said as the ability to recognize the object along with all its details, such as id or location at runtime. One of the most basic introspects we came across many times earlier is type()


We used it to see the type of our object, that whether it is int, float or string. We have to pass the object in the parenthesis, and the compiler will return the type. Introspection gives us useful information about the program’s objects. Python provides tremendous introspection support. Introspection is an ability to determine the type of an object at runtime. Henceforth, by using introspection, we can inspect the Python objects dynamically.

There are many types of introspects. In this tutorial, we will focus on three of them so you may get a brief idea about their working. You may search the internet for more, but for conceptual learning, we will be focusing on three. We have already discussed type( ), now let’s move onto id( ). Id provides us with the id allocated to the particular object. The id of each object is unique, meaning it is different, and no two objects can have the same id. 


Now the most important introspection function is dir(). It returns us a list of attributes and methods associated with an object. By using dir(), we can check the attributes that our object is composed of. It is mostly executed before and after updating our object by inserting more attributes or methods. 

o = MyClass()

Types of introspects:-

Some of the other common Introspects:

 Functions Working
hasattr() It checks if an object has an attribute.
getattr() It returns the contents of an attribute if there are some.
repr() It returns the string representation of an object
vars() It checks all the instance variables of an object
issubclass() This function checks that if a specific class is a derived class of another class.
isinstance() It checks if an object is an instance of a specific class. 
__doc__ This attribute gives some documentation about an object 
__name__ This attribute holds the name of the object
callable() This function checks if an object is a function
help() It checks what other functions do


In this tutorial, we have learned about object introspection. It is an ability to determine the type of an object. In python, everything is an object. By using object introspection, we can dynamically examine python objects.



Code as described/written in the video

class Employee:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        # = f"{fname}.{lname}"

    def explain(self):
        return f"This employee is {self.fname} {self.lname}"

    def email(self):
        if self.fname==None or self.lname == None:
            return "Email is not set. Please set it using setter"
        return f"{self.fname}.{self.lname}"

    def email(self, string):
        print("Setting now...")
        names = string.split("@")[0]
        self.fname = names.split(".")[0]
        self.lname = names.split(".")[1]

    def email(self):
        self.fname = None
        self.lname = None

skillf = Employee("Skill", "F")
# print(
o = "this is a string"
# print(dir(skillf))
# print(id("that that"))

import inspect


hellosh 3 months, 1 week ago
Thank You Sir


No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here