Python is a very versatile and object-oriented language as it Supports OOP concepts like Classes and objects in the program. Classes make our code arranged in well-defined structures. In Python, classes are shareable and can be reused anywhere in the program. We also utilize different advantages of classes in the program, such as polymorphism, abstractions, and encapsulation.
This article explains the below-listed methods to get the name of the class in Python:
How to Get Class Name in Python?
In Python, the class is a constructor of an object. Classes are the main features of OOP, and objects are created using classes. The “class” prefix is used before the class name to create the classes in Python. Data and members’ methods are available in classes. The object is described by the class, but the class can not describe using the object.
Different objects are built using a single class. The class is specified first, and its objects are defined using the class’s attributes. The class syntax is given below:
class MyClass:
The class name “MyClass” is created using the keyword “class”.
Method 1: Get Class Name Using type() with __name__
In Python, the “type()” method is mainly used to find the data type of any variable, but we can use this function to find the class name with the help of attributes “__name__”. The “type()” with “ __name__” shows the name of the module or class which we are currently using.
Any class name can be obtained using the type() function with attributes of “__name__”. Let’s understand this with an example of code given below:
Code
class school:
def __init__(self, students):
self.students = students
a = school("Ali")
print (type(a).__name__)
In the above code:
- The class name “school” is initialized.
- The “__init__” constructor defines the attributes of the school in the parameter with the object “self” and variable “students”.
- The instance variable “self.students” is created.
- The name of students is saved in the variable “a”.
- The“type()” with “__name__” is used to get the main class name.
Output
The class name “school” is obtained using the “type().__name__” method.
Method 2: Get Class Name Using __class__ and __name__
The simplest and easiest way to get the class name is by using the “__class__” property combined with the “__name__” property. The “__name__” is a special python variable used along with the “__class__” attribute.
Let’s understand this with an example:
Code
#Using __class__.__name__
class school:
def __init__(self, students):
self.students = students
a = school("Ali")
print (a.__class__)
print (a.__class__.__name__)
In the above code:
- Class name “school” is initialized.
- The “__init__” constructor parameter called the variable name “students” and object “self”.
- The “__class__” with “__name__” attributes can get the name of the class.
Output
The class name “school” is obtained in the above output.
Method 3: Get the Class Name Using Nested classes
In Python, we can also call the nested class or class within the class using the “__qualname__” property. The “__name__“ is used to call the name of the nested class of the module, while with the help of “__qualname__” we can obtain the name of the main class of the current module.
Let’s understand this with an example given below:
Code
class School:
def __init__(self, name, z):
self.name = name
self.students = self.students(z)
class students:
def __init__(self, students):
self.students = students
School = School("junior", ['senior'])
print(School.students.__class__.__name__)
print(School.students.__class__.__qualname__)
In the above code:
- The class name “School” is initialized.
- The nested class name “students” is initialized within the main class.
- The “__qual name__” attribute gives more complete information about the class than the simple “__name__” attributes. It is used to get the name of the main class when there is more than one class found in the module.
- The “__qual name__” attributes along with “__class__” is used to find the main class name “School”.
Output
Here, the nested class name “students” and main class name “School” is obtained.
Conclusion
In Python, the class name is obtained by different methods like “type().__name__”, “__class__.__name__” and “nested “__qualname__” method. These methods are efficient and simple to obtain the class name. The “type().__name__” and “__class__.name__” methods get the name of the class or instance of the current module. The “__qualname__” attribute is used to get the name of the main class when nested classes are present in the program. There are multiple benefits of OOP in Python as it makes the code error-free and builds a well-defined structure of code in Python. This article covers all the methods in detail with an appropriate example to get the class name in Python.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY