Skip to content
Related Articles

Related Articles

Python Program to Get the Class Name of an Instance

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Aug, 2022

In this article, we will see How To Get a Class Name In Python instances. 

For getting the class name of an instance we have the following 4 methods that are listed below:

  • Using the combination of the __class__ and __name__ to get the type or class of the Object/Instance.
  • Use the type() function and __name__ to get the type or class of the Object/Instance.
  • Using the decorator to get the type or class of the Object/Instance.
  • Using nested classes to get the type or class of the Object/Instance.

What is the class name?

A user-defined class serves as a template or prototype from which objects can be built. Classes offer a way to group together functionality and data. A new class produces a new type of object, enabling the creation of new instances of that type.

Method 1: Using __class__.__name__ 

__name__ is one such special variable. It is a built-in variable that evaluates the name of the current module. Thus it can be used to check whether the current script is being run on its own or being imported somewhere else by combining it with the if statement. The type() function and the __name__ variable are used to get the type or class of the object in the code:

Python3




# this is a class named car
class car:
    def parts():
        pass
 
c = car()
 
# this prints the class of a c
# this is a class of a c which is
# variable containing a class
print(c.__class__)
 
# this prints the name of the class
classes = c.__class__
print(classes.__name__)

Output:

<class '__main__.car'>
car

Method 2: Using type() and __name__ attribute

Also, we can also print the type of c (class car) which gives the object of c and __name__ provides the name of the class. __name__ is a built-in variable that evaluates the name of the current module.

Python3




# this is a class named car
class car:
    def parts():
        pass
 
 
c = car()
 
# this prints the class of a c
print(type(c).__name__)

Output:

car

Method 3: Using a decorator

Here we used @property decorator in order to get a name other than a python method. To get more information about @property decorator please refer to Python Property Decorator. The function which returns the name of the class

Python3




# class for getting the class name
class test:
    @property
    def cls(self):
        return type(self).__name__
 
 
a = test()
print(a.cls)

Output:

test

Method 4: Using nested classes

In this example, we can obtain the name of the class object by using the __qualname__ attribute rather than the __name__ attribute. The __qualname__ provides a dotted path to the target object’s name. When dealing with nested structures, such as when a method is contained within a class, using __qualname__ is beneficial.

Python3




class bike:
    def __init__(self, name, b):
        self.name = name
        self.car = self.car(b)
 
    class car:
        def __init__(self, car):
            self.car = car
 
 
vehicle = bike("orange", ['potatoes'])
 
print(vehicle.car.__class__.__name__)
print(vehicle.car.__class__.__qualname__)

C++




#include <iostream>
using namespace std;
 
int main() {
 
    cout << "GFG!";
    return 0;
}

Output:

car
bike.car

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!