What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Get Class Name in Python?

  • Dec 28, 2021
  • 5 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Shivali Bhadaniya
How to Get Class Name in Python?

 

Python is well famous to support object-oriented programming including the concepts of classes and objects. It provides a clear program structure and easy modification of code. Since the class is sharable, the code can be reused and also provide different advantages of abstractions, encapsulation, and polymorphism. In this article, we will study the python class and how python get class name of an instance with examples and output. Let’s get started!

What is Class in Python?

Classes are the focal point of OOP and objects are created by Classes. The class can be defined as the description or the definition of the object. The class describes what the object will be but is totally separated from the object itself.

Also, a single class is used to describe multiple objects. For example, we create a blueprint before constructing the house. Similarly, the same blueprint can be used for different houses to be built. The programming works in the flow where first the class is defined which further describes the object. Each class has a name, and its own attribute and behavior.

The method is another term used in classes. Methods are the functions in the classes. They have a specific block of code that can be called and execute a certain task and return the values. The syntax of the class is as given by:

Syntax

class ClassName:
    # Statement1
    .
    .
    .
    # StatementN

 

For example:

class Fruit:
  def __init__(self, name, color):
    self.name = name
    self.color = color

  def func(self):
    print("Fruit is " + self.name)

f1 = Fruit("Apple", "Red")
f1.func()

 

Output:

Fruit is Apple

 

Methods to Get Class Name in Python

Below are 3 methods by which you can get the class name in python: 

1) Using __class__.__name__

The first and easiest method to get a class name in python is by using __class__ property which basically refers to the class of the object we wish to retrieve. Here we combine the property with __name__ property to identify the class name of the object or instance. __name__ is the special variable in python that provides the name of the current module where it is used.

Note that to display the class name you have to create the object for that lass and display its name using class.name. Check out the below example for a better understanding of these methods.

For example:

class fruit:
    def __init__(self, fruit):
        self.fruit = fruit
a = fruit("orange")
print (a.__class__)
print (a.__class__.__name__)

 

Output:

<class '__main__.fruit'>
fruit

 

2) Using type() and __name__attribute

Another method is to use the type() method which is the in-built python method to find the type or the class name of the object. You have to merge the type() function with the __name__ variable and get the name of the class as shown in the below example:

For example:

class fruit:
    def __init__(self, fruit):
        self.fruit = fruit
a = fruit("orange")
print (type(a).__name__)

 

Output:

fruit

 

3) Using nested classes

There are scenarios where you have nested classes in the program and wish to get a class name. In this situation, you can make use of __qualname__ attribute instead of __name__ attribute to get the name of the qualified object.

For example:

class Fruit:
    def __init__(self, name, b):
        self.name = name
        self.vegetable = self.Vegetables(b)
 
    class Vegetables:
        def __init__(self, vegetable):
            self.vegetable = vegetable
 
 
fruit = Fruit("orange", ['potatoes'])

print(fruit.vegetable.__class__.__name__)
print(fruit.vegetable.__class__.__qualname__)

 

Output:

Vegetables
Fruit.Vegetables

 

Summary

Object-oriented programming in python provides multiple advantages such as abstraction, polymorphism, etc. It helps to maintain your code easily and error-free providing the better structure and ability to reuse it. This article represents the various methods by which python get class name easily using __class__, type() and __qualname__ methods. It is recommended to learn and understand this method thoroughly and make your programming easy and efficient.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.