Python Program to Get the Class Name of an Instance

Updated on 15 May, 2025
Get the class name of an instance header image

Introduction

Identifying an object’s class in Python isn’t just helpful but essential, especially in dynamic or object-oriented codebases where an instance’s type isn’t always obvious. Knowing the class behind an object allows you to debug more effectively, log meaningful information, validate input types, and implement polymorphic behavior. Once you get the class name of an instance, you gain a clearer understanding of its capabilities, helping you write more robust, adaptable, and maintainable code.

In this article, you will learn how to get the class name of an instance in Python through practical examples. Explore conventional methods as well as Python's introspective capabilities to achieve this.

Get Class Name of Object Using the type() Function in Python

Basic Usage of type()

  1. Create an instance of a custom class.

  2. Use the type() function to get the type of the instance.

  3. Extract the class name from the type object.

    python
    class Vehicle:
        pass
    
    my_car = Vehicle()
    class_type = type(my_car)
    class_name = class_type.__name__
    print(class_name)
    

    The type() function in this Python program returns a type object from which __name__ attribute provides the class name 'Vehicle'.

Applying type() in a Function to get Class Name

  1. Define a function that takes any object as an input.

  2. Use the type() function to determine the object’s class name inside the function.

  3. Print or return the class name.

    python
    def get_class_name(obj):
        return type(obj).__name__
    
    class Fruit:
        pass
    
    apple = Fruit()
    print(get_class_name(apple))
    

    This function, get_class_name, works universally for any object passed to it. For the instance of Fruit, it returns 'Fruit'.

Using __class__ Attribute to Get Class Name in Python

Direct Access of __class__ Attribute

  1. Create an instance of a class.

  2. Access the __class__ attribute of the instance.

  3. Retrieve the name of the class from the __class__ attribute.

    python
    class Computer:
        pass
    
    my_computer = Computer()
    class_name = my_computer.__class__.__name__
    print(class_name)
    

    By accessing __class__ directly, the script returns the class name 'Computer' for the instance my_computer.

Use __class__ in More Complex Class Structures

  1. Define a class with inheritance.

  2. Create an instance of the subclass.

  3. Use __class__ to find out the name of the most specific class.

    python
    class Animal:
        pass
    
    class Dog(Animal):
        pass
    
    my_dog = Dog()
    print(my_dog.__class__.__name__)
    

    Even with inheritance, __class__ accesses the subclass name 'Dog', demonstrating effective resolution in complex class hierarchies.

Conclusion

Efficiently retrieving the class name of an instance in Python provides you with rhetorical capabilities in your code, especially when handling an ecosystem of varied object types and implementations. Both the type() function and the __class__ attribute offer robust solutions to identify the class of an instance reliably in Python. Adapt these methods in debugging scenarios, type checking, or when implementing polymorphic behavior in your applications, improving both versatility and code manageability. The understanding and implementation of these techniques ensure that your Python programming becomes more insightful and controlled.

Comments

No comments yet.

Python Program to Get the Class Name of an Instance | Vultr Docs