To get an instance’s class name, you can use __class__.__name__ method in Python.
Getting the class name of an instance is a useful operation in Python for debugging, logging, and mainly for dynamic programming. This helps in deciding on the type of object to be used. In this blog, let’s explore the multiple approaches to get the class name of an instance in Python.
Table of Contents:
Methods to Get the Class Name of an Instance in Python
Python has various methods to get the class name of an instance:
Method 1: Using __class__.__name__ in Python
This method directly refers to the class name of the object via the __class__ attribute.
Example:
Output:

Explanation: The __class__ gives the reference to the class of the object and the __name__ extracts the name of the class and returns Intellipaat.
Method 2: Using type() and __name__ in Python
The type(object) provides the class of the object, which refers to the class in the attribute, and the __name__ gives you the name of the class in string format.
Example:
Output:

Explanation: The type(object) returns the type of the object (class) and __name__ gets the class name and returns ‘IntellipaatCourses’.
Method 3: Using a Decorator to get the class name in Python
The class decorator with @property gives access to the attributes and returns the class name without the necessity of calling the method.
Example:
Output:

Explanation: The @property decorator converts the cls method into a property that returns the class name of the object. When you access a.cls, it prints the name of the class as ‘Intellipaat_Guide’.
Method 4: Using Nested Classes to get the name of the class in Python.
The Nested classes to get the name of the class in Python. The Nested class method uses __qualname__ to give the full path to the class. It provides a detailed reference as compared to __name__.
Example:
Output:

Explanation: A class called ‘Intellipaat’ has been designed with an inner class, ‘Blogs’. __Name__ gives a name of the nested class; __qualname__ gives it with its fully qualified name, including the name of the outer class.
Method 5: Using the __qualname__ Attribute in Python
The __qualname__ attribute gives fully qualified names for the classes, with respect for their scoping concerning nested classes.
Example:
Output:

Explanation: The __qualname__ attribute gives the class names full bore along with their parents, if nested. Here, it returns “Parent.Child” as Child is contained inside Parent.
Performance Comparison for each method
Method | Use Case | Approach | Performance |
Using __class__.name__ | Accessing the class name directly. | Direct attribute access (no function calls). | fastest |
Using type(obj).__name__ | Obtaining the type of object | Uses type() function + attribute access. | fastest |
Using a Decorator (@property) | Returning class name as a property. | Encapsulates logic via a property decorator. | moderate |
Using Nested Classes with __qualname__ | Using the name attribute to get the name of the class, sometimes with full paths. | Accesses nested class names via __name__. | Slightly slower |
Using __qualname__ Attribute | The__qualname__ attribute also returns the full class hierarchy. | Provides fully qualified names including its parent classes. | Slightly slower |
Real-World Use Cases
Case 1: Logging and Debugging: In logging instances of exceptions or system events, the class can determine the cause of the issue that occurs in that instance. It will be highly beneficial in complex codes, working with plenty of classes that would include identical operations and behaviors. For example, in handling exceptions, the retrieval of class names would provide greater detail to the logs.
Case 2: Serialization and Deserialization: While deserializing the objects into XML or JSON, if the class names are being saved, then the right class instance would be reconstructed for deserialization. So, APIs such as Pickle depend on class names for recognizing objects.
Case 3: Dynamic Method Invocation: In reflective programming, methods are invoked dynamically at runtime. Thus, getting the class names will also help in accessing methods and fields, common use cases are in dynamic behavior implementation and plugins.
Case 4: Automated Testing and Mocking: When we are using mock frameworks like unittest.mock, we need to know the correct name of a class so that it helps us create appropriate mocks. Then the environment will perfectly mirror reality.
Conclusion
Obtaining an instance’s class name has certain advantages, such as debugging, logging, or type checking. The easiest class name that can be fetched is via __class__.__name__. The other methods are @property decorator and __qualname__ to be helpful in nested cases. All these methods are explained to fetch class names effectively in Python.
FAQs
1. What is the simplest way to get the class name of an instance?
The simplest way is using an instance .__class__.__name__, which directly returns the class name as a string.
2. Can I use the type() function to get the class name?
Yes, using type(instance).__name__ is another straightforward method to retrieve the class name.
3. When should I use the __qualname__ attribute?
The __qualname__ attribute is useful when dealing with nested classes, as it returns the fully qualified name, including the outer class.
4. Is there a performance difference between these methods?
Yes, accessing __class__.__name__ and type().__name__ are generally faster than methods involving decorators or accessing __qualname__.
5. Why use a decorator to get the class name?
Using a decorator allows adding custom logic when retrieving class names, which can be useful for logging or tracking instances dynamically.