Getting the class name of an instance in Python

Home /

Table of Contents

Glance on Python ‘Class’

A class in Python is a blueprint for creating objects. It defines a set of attributes and behaviors that objects of the class will have. A class is essentially a template for creating objects, and each object created from the class is called an instance of the class.

Classes allow you to encapsulate data and behavior, and provide a way to structure and organize code. They allow you to create reusable code, making it easier to write, maintain and extend your code.

Here’s a simple example of a class definition in Python:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    def start_engine(self):
        print("Engine started")
    def stop_engine(self):
        print("Engine stopped")

In this example, ‘Car‘ is the class name, ‘make‘, ‘model‘, and ‘year‘ are the attributes of the class, and ‘start_engine‘ and ‘stop_engine‘ are the methods of the class. To create an instance of the ‘Car‘ class, you would call ‘Car()‘ and pass any required arguments, such as the make, model, and year of the car.

Determine which Python ‘class’ is used to generate an instance of an object

To determine the class of an object in Python, you can use the ‘type‘ built-in function. The ‘type‘ function returns the type of the object passed as an argument.


class MyClass:

obj = MyClass()


<class '__main__.MyClass'>

You can also use the ‘isinstance‘ function to check if an object is an instance of a specific class or a subclass of it.


class MyParentClass:

class MyChildClass(MyParentClass):

obj = MyChildClass()

print(isinstance(obj, MyParentClass))
print(isinstance(obj, MyChildClass))



Create instance of an object

In Python, you can create an instance of a class by calling the class’s constructor method, which is usually the ‘__init__‘ method. Here’s an example:

class Person:
    def __init__(self, name, age): = name
        self.age = age

person1 = Person("Rahim", 30)
person2 = Person("Karim", 40)

In this example, we define a class Person with an ‘__init__‘ method that takes two arguments: ‘name‘ and ‘age‘. The method initializes two instance variables, ‘‘ and ‘self.age‘, with the values passed as arguments.

To create instances of the ‘Person‘ class, we simply call the constructor method and pass in the necessary arguments. In this case, we create two instances, ‘person1‘ and ‘person2‘, with different names and ages.

Once you have created an instance of a class, you can access its attributes and methods using the dot notation. For example, you can access the ‘name‘ attribute of ‘person1‘ like this:


This will output:


Similarly, you can call the ‘__init__‘ method of ‘person1‘ like this:

person1.__init__("Sifat", 50)
print(, person1.age)

This will output:

Sifat 50

It’s generally not a good idea to call the ‘__init__‘ method directly like this, but it’s possible if you need to change the values of the instance variables after the object has been created.

Overall, creating an instance of a class is a simple process in Python, and allows you to create multiple objects that share the same attributes and methods.

Share The Tutorial With Your Friends

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article