Get the Class Name of an Instance in Python

Welcome to our tutorial on how to get the class name of an instance in Python! In this tutorial, we will be discussing the various ways to get the class name of an instance in Python, using a simple example of a class called “Newtum”.

The first step in getting the class name of an instance is to define a class. In this example, we have defined a class called “Newtum“, which has a method called “name()” that takes one argument, “name”, and returns that name. Once we have defined the class, we can create an instance of that class. In this example, we are creating an instance called “n” of the “Newtum” class.

Python Program to Get the Class Name of an Instance

To get the class name of an instance, we can use the __class__ attribute of the instance. The __class__ attribute returns the class object of an instance, from which you can get the class name using the __name__ attribute. We can also use the built-in “type()” function to get the class name of an instance in Python. The “type()” function takes an object as an argument and returns the type of the object.

Let’s recall:

Step 1: Understand the Objective

The objective of this tutorial is to demonstrate how to get the class name of an instance in Python. The provided code is a simple example to showcase the method to achieve this.

Step 2: Define the Class

The first step is to define the class. In this example, we will use the class name “Newtum”. We define the class using the “class” keyword followed by the name of the class. In this case, we define the class with a single method called “name”.

Step 3: Create an Instance of the Class

After defining the class, we need to create an instance of the class. In this example, we create an instance of the class Newtum and store it in a variable called “n”.

Step 4: Get the Class Name of the Instance

To get the class name of the instance, we use the “class. name” attribute. We access this attribute using the instance variable “n” and print the result.

Step 5: Run the Program

After following the above steps, the Python program should look like the following:

# Python Program to Get the Class Name of an Instance

class Newtum:
	def name(self, name):
    	return name

# Create object n of class Newtum()
n = Newtum()

# Print the name of the class using __class__.__name__
print(n.__class__.__name__)

Output:

Finally, we can print the class name using the “print()” function. Save the program to a Python file and run it. The output of the program should be as follows:

Newtum

This output confirms that we have successfully got the class name of the instance “n”. It’s worth noting that, __class__ is a special attribute of every Python object that holds a reference to the class object of the instance, and it can be used to access (get) the class of an instance.

In conclusion, getting the class name of an instance in Python is a simple task that can be achieved using the __class__ attribute. With this example, you have seen how to define a class, create an instance of that class, and get the class name of an instance in Python.

Understanding how to get the class name of an instance is an important part of understanding object-oriented programming in Python and can be used in various situations when working with instances of classes.

Congratulations, you have successfully completed all the steps and learned how to get the class name of an instance in Python. You can now use this method in your Python programs to get the class name of any instance.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply