Getting the class name of an instance in Python?

Better Stack Team
Updated on February 3, 2023

You can use the built-in type() function to get the class name of an instance in Python. For example:

 
class MyClass:
    pass

my_instance = MyClass()
print(type(my_instance).__name__)

This will output MyClass. You can also use the __class__ attribute on the object which returns the class of the instance.

 
class MyClass:
    pass

my_instance = MyClass()
print(my_instance.__class__.__name__)

This will also output MyClass.