Python Program - Get the Class Name of an Instance

www.igif‮aedit‬.com

here's a Python program that gets the class name of an instance:

# define a class
class MyClass:
    pass

# create an instance of the class
my_instance = MyClass()

# get the class name of the instance
class_name = my_instance.__class__.__name__

# print the class name
print("The class name is:", class_name)

In this program, we first define a simple class called MyClass using the class keyword.

We then create an instance of the class using the class name followed by parentheses, and store it in the variable my_instance.

We use the __class__ attribute to get a reference to the class of the instance, and then use the __name__ attribute to get the name of the class as a string. We store the class name in the variable class_name.

Finally, we use the print() function to print out the class name.