Built with Tutify.io

    AIStudyLabs

    Python Get Class Name of an Instance

    Python Get Class Name of an Instance

    You would some time required to know the class name of a given instance during the Python runtime. The best solution to get the class name of an instance is the type() function and name attribute.

    There are other ways though, for getting the class name of an instance. In this article, we will discuss different methods for getting the class name of an instance with examples

    <a id="h-1-quick-examples-of-getting-class-name-of-an-instance"></a>

    1. Quick Examples of Getting Class Name of an Instance

    These quick examples will give you a high-level overview of the different methods for getting the class name of an instance. We will discuss them in detail.

    
    # Quick examples of getting Class name of an instance
    
    # Define a class
    class Person:
        def __init__(self, name):
            self.name = name
    
    # Create an instance 
    person = Person('Ali')
    
    # Class name using the type() 
    class_name = type(person).__name__
    print(class_name)  
    
    # Class name using the class attribute 
    class_name = person.__class__.__name__
    print(class_name)  
    
    # Class name using the __qualname__ attribute
    class_name = person.__class__.__qualname__
    print(class_name)
    

    <a id="h-2-type-name-get-class-name-of-instance"></a>

    2. type().name - Get Class Name of Instance

    This is the most common method to get the name of the class of an instance in python. The type() function is a built-in Python function that returns the type of an object. You have to pass the instance to type() function and it will return the class of the object.

    You can then use the __name__ attribute of the class object to get the name of the class. See the following example:

    
    # Get class name of instance
    x = 59
    y = "String"
    z = 4.23
    
    # Get the class name of each instance 
    x_class = type(x).__name__
    y_class = type(y).__name__
    z_class = type(z).__name__
    
    # Print the class names
    print(x_class)  
    # Output: 'int'
    
    print(y_class)  
    # Output: 'str'
    
    print(z_class)  
    # Output: 'float'
    

    <a id="h-3-class-name-using-the-name-attribute"></a>

    3. class.name - Using the name attribute

    When we create a class in Python, it has a default name that is generated, if we don't explicitly assign a name to it. We can access this default name by using the class.name attribute of the class.

    See the following Example:

    
    # Get the custom class name example
    
    # Create class
    class MyClass:
        pass
    
    # Get class name
    class_name = MyClass.__name__
    print(class_name)  
    
    # Output: 
    # 'MyClass'
    

    The MyClass class has a default name of 'MyClass', which we can access using the class.name attribute as follows:

    If we want we can override the default name of the class and have our name for the class using the __name__ attribute. See the below example.

    
    # Get instance of class example
    class MyClass:
        __name__ = "MyNewClass"
    
    # Create instance of class
    obj = MyClass()
    
    # Get class name of instance
    class_name = obj.__class__.__name__
    print(class_name) 
     
    # Output: 'MyNewClass'
    

    The __name__ attribute is also available for the built-in classes as well. See the following example for int, float , and str.

    
    # Using the class.name attribute
    x_class_name = x.__class__.__name__
    y_class_name = y.__class__.__name__
    z_class_name = z.__class__.__name__
    
    # Print the class name of each instance
    print(x_class_name)  
    # Output: 'int'
    
    print(y_class_name)  
    # Output: 'str'
    
    print(z_class_name)  
    # Output: 'float'
    

    <a id="h-4-class-qualname-qualified-class-name-of-object"></a>

    4. class.qualname - Qualified Class Name of Object

    The qualified class name is the fully qualified name of a class, including the names of any enclosing classes and the module in which the class is defined.

    Python also provides a __qualname__ attribute, which returns the fully qualified name of the class. See the below code example.

    
    class OuterClass:
        class InnerClass:
            pass
    
    obj = OuterClass.InnerClass()
    class_name = obj.__class__.__qualname__
    print(class_name)  
    
    # Output: 'OuterClass.InnerClass'
    

    <a id="h-5-inspect-module-inspect-the-instance-of-a-class"></a>

    5. inspect Module - Inspect the Instance of a Class

    Using inspect.getmembers() of the inspect module is another solution to get the class name of an object. We can pass the instance to the function along with a filter function that selects only attributes of type type.

    The resulting list will include all attributes of the instance that are classes, including the class of the instance itself. We can then access the __name__ attribute of the class to get its name.

    
    # Using inspect module
    import inspect
    
    int_object = 5
    
    ins_list =inspect.getmembers(int_object, lambda x: isinstance(x, type))
    
    for name, obj in ins_list:
        print(name, obj.__name__)
    

    <a id="h-6-summary-and-conclusion"></a>

    6. Summary and Conclusion

    There are several ways to get the class name of an instance in Python, we have explained some of the most common methods including using the type() function, accessing the __class__.name attribute, and using the inspect module. I hope this article was helpful. Please, leave your questions in the comment section below.

    Happy Coding!

    <a id="h-related-articles"></a>

    Related Articles