Get the Class Name of an Instance in Python

(Last Updated On: 11/11/2022)

Here is a simple python program to get the class name of the instance. The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing program. Sometimes you write a program with functions that might be useful in other programs as well.

Python Program to Get the Class Name of an Instance

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

Newtum

Leave a Reply

Your email address will not be published. Required fields are marked *