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 : 31 Aug, 2022
View Discussion
Improve Article
Save Article

In this article, we will see How To Get a Class Name of a class instance.

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.

Using __class__.__name__ to Get the Class Name of an Instance

__name__ is a special variable in Python. 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.

Python3




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


Output:

<class '__main__.car'>
car

Using type() and __name__ attribute to Get the Class Name of an Instance

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(self):
        pass
 
 
c = car()
 
# this prints the class of a c
print(type(c).__name__)


Output:

car

Using a decorator to Get the Class Name of an Instance

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

Using nested classes to Get the Class Name of an Instance

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__)


Output:

car
bike.car

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!