Python Program to Get the Class Name of an Instance

Spread the love

In Python, everything is an object, whether it’s a string, a list, a dictionary, or an instance of a user-defined class. Often, while working with these objects (instances), it becomes necessary to know the class to which an instance belongs, primarily when dealing with multiple classes and large codebases. This article will explore various methods to retrieve the class name of an instance in Python and delve into its importance and application in different domains.

Method 1: Using __class__.__name__ Attribute:

Python objects have a __class__ attribute that allows access to the class to which the object belongs. The class object further has a __name__ attribute containing the class’s name as a string.


class SampleClass:

instance = SampleClass()
class_name = instance.__class__.__name__
print(class_name)  # Output: SampleClass

Method 2: Using type( ) function:

The type() function can also be used to get the type of an object, and subsequently, the class name can be retrieved using the __name__ attribute.


class AnotherClass:

instance = AnotherClass()
class_name = type(instance).__name__
print(class_name)  # Output: AnotherClass

Method 3: Using inspect Module:

The inspect module provides several useful functions to get information about live objects such as modules, classes, methods, etc. The getmro() function can be used to retrieve the class name from the method resolution order of the instance.


import inspect

class ExampleClass:

instance = ExampleClass()
class_name = inspect.getmro(instance.__class__)[0].__name__
print(class_name)  # Output: ExampleClass

Importance of Retrieving Class Name:

  1. Debugging: Knowing the class name of an instance is crucial for debugging, especially when dealing with a large codebase or multiple classes.
  2. Logging: Recording the class names in logs can assist in tracing the flow of execution and identifying the sources of errors or exceptions in the code.
  3. Reflection: In reflective programming, identifying the class name dynamically is essential for inspecting and modifying objects, classes, and functions at runtime.
  4. Object Serialization: While serializing objects, storing the class name is important for reconstructing the object during deserialization.
  5. Type Checking: Checking class names allows developers to perform type verification and enforce type safety in a program, ensuring the correct usage of objects.

Precautionary Notes:

  1. Name Mangling: For classes with a name starting with an underscore, Python performs name mangling, and special consideration is required to access the correct class name.
  2. Inheritance: When dealing with inherited classes, developers should ensure that the retrieved class name is of the actual class type, considering the inheritance hierarchy.
  3. Metaclasses: When using metaclasses, the retrieval of class names might differ, and the approach should be chosen carefully to avoid discrepancies.


Retrieving the class name of an instance in Python is a valuable capability, especially in complex systems involving multiple classes and extensive codebases. It facilitates debugging, logging, reflection, and type checking, enhancing the robustness and reliability of Python programs.

Whether using the inherent attributes like __class__.__name__ or employing the type() function or leveraging the advanced inspect module, Python offers diverse methods to suit various needs and scenarios.

Leave a Reply