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.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →