How to get the class name of an instance in Python?


The object-oriented procedural programming, including the ideas of classes and objects, is well supported by Python. It offers a crystal-clear program structure and simple code modification. The code can be reused and offers many benefits of abstractions, encapsulation, and polymorphism due to the class's shareability.

A Python class is a "blueprint" for creating objects in Python is a class.

Getting Class name of an instance in Python

To determine a Python instance's class name there are two ways −

  • Using the type() function and __name__ function.

  • Using the combination of the __class__ and __name__.

A unique built-in variable called __name__ basically provides the name of the current module in which it is used.

Since Python doesn't have a main() method like other programming languages like C/C++, Java, and others that are similar, the interpreter sets the value of __main__ to __name__ if the source file is used as the main program.

In addition, __name__ is set to the name of the importing module if a file is imported from another module.

The following code retrieves the type or class of the object using the type() function and the __name__ variable

Example

class number: def __init__(self, number): self.number = number x = number(1) print (type(x).__name__)

Output

number

In this article, we wil learn to get the class name of an instance in Python using the above mentioned methods.

Using __class__ .__name__

Python's __class__ property, which simply refers to the class of the object we want to retrieve, is the first and simplest way to get a class name. Here, we combine the property with the __name__ property to determine the object's or instance's class name. The unique Python variable __name__ specifies the name of the current module in which it is utilised.

Note − You must create the object for that class in order to display its name using class.name.

Example

Following is an example to get the class name of an instance in Python using __class__.__name__ method:

class animal: def __init__(self, animal): self.animal = animal l = animal("Lion is the animal") print (l.__class__) print (l.__class__.__name__)

Output

Following is an output of the above code −

<class '__main__.animal'>
animal

Using type() and __name__ attribute

A different approach is to utilise Python's built-in type() method to determine the object's type or class. To obtain the class name, you must combine the type() function with the __name__ variable.

Example

Following is an example to get the class name of an instance in Python using type() and __name__ attribute −

class animal: def name(self, name): return name a = animal() print(type(a).__name__)

Output

Following is an output of the above code −

animal

Example

Let's examine the class name of a simple string as shown in the following example −

d = "Python Program" print(type(d).__name__)

Output

The class of a string instance is “str”, as you can see in the following output −

str

Example

Use the “count” instance of the itertools module to acquire the class name as shown in the following example −

import itertools d = itertools.count(2) print(type(d).__name__)

Output

Following is an output of the above code −

count

Example

Find the class of the “lists” instance by defining an empty list as shown in the example mentioned below −

the_list = [] t = type(the_list) name_of_class = t.__name__ print(name_of_class)

Output

Following is an output of the above code −

list

Example

The class name is also available as a string as shown in the following example −

class S: pass d = S() print(str(d.__class__))

Output

Following is an output of the above code −

<class '__main__.S'>

Using nested classes (using __qualname__ instead of __name__)

In some cases, you may want to get the class name in a program that has nested classes. In this case, you can retrieve the name of the qualified object by using the __qualname__ attribute rather than the __name__ attribute.

Example

There are two classes listed: "Animal" and "Lion" . Additionally, as shown in the code below, we have created the variable instances for both classes. The Lion's object is called inside the class "Animal" and given an argument named "a" that is a variable.

The values for the instance Animal and Lion are then set in the class "Animal" object "obj_Animal". Both the method "__name__" and the method "__qualname__" are available in the print function to retrieve the class name and the qualified object name, respectively.

class Animal: def __init__(self, a_name, a): self.a_name = a_name self.obj_Lion = self.Lion(a) class Lion: def __init__(self, obj_Lion): self.obj_Lion = obj_Lion obj_Animal = Animal("Sher", "King") print(obj_Animal. obj_Lion.__class__.__name__) print(obj_Animal. obj_Lion.__class__.__qualname__)

Output

Following is an output of the above code −

Lion
Animal.Lion

Updated on: 23-Nov-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements