How to Get Class Name of an Instance in Python

Python get class name: This tutorial will explain you how to get the name of a python class.
What is a Python Class ?
In python language, a class is the only way to define new types specific to the programmer. It is quite possible to do without classes to write a computer program. Their use nevertheless improves its presentation and the understanding that one can have of it. Classes can be created using the keyword class
.
Here is an example of a class definition in Python :
class Superhero:
def __init__(self, name, p):
self.name = name
self.power = self.Power(p)
class Power:
def __init__(self, power):
self.power = power
superhero = Superhero("Superman", ['X-rays vision', 'superhuman strength', 'super-speed'])
In some cases, it will be important to know the type of the class when instantiating an object. We will use the class above to show you the different methods to do this.
Python Get Class Name: Using __class__.__name__
To get the class name of an object or its type, the first method is to use the __class__ property. We can combine this property with the __name__ property to get the name of the class of an object. Here is how to do it:
class Superhero:
def __init__(self, name, p):
self.name = name
self.power = self.Power(p)
class Power:
def __init__(self, power):
self.power = power
superhero = Superhero("Superman", ['X-rays vision', 'superhuman strength', 'super-speed'])
print(superhero.__class__)
print(superhero.__class__.__name__)
Output:
<class '__main__.Superhero'>
Superhero
To display the name of the class you have to :
- Create an object superhero of the class Superhero().
- Display the name of the class using class.name.
Python Get Class Name: Using type() and __name__ attribute
Another method is to use the type() function. The type() function is a predefined function in python that is used to find out the type or class name of an object.
We can combine the type() function with the __name__ variable to get the name of a class:
class Superhero:
def __init__(self, name, p):
self.name = name
self.power = self.Power(p)
class Power:
def __init__(self, power):
self.power = power
superhero = Superhero("Superman", ['X-rays vision', 'superhuman strength', 'super-speed'])
print(type(superhero).__name__)
Output:
Superhero
The __name__ variable is a special variable built into python that gives the name of the current module where it is used.
Nested Classes: Using __qualname__ instead of __name__
In some circumstances, it is possible to have nested classes. In this case, it is preferable to use the __qualname__ attribute rather than __name__ to get the qualified name of the type.
Here is an example:
class Superhero:
def __init__(self, name, p):
self.name = name
self.power = self.Power(p)
class Power:
def __init__(self, power):
self.power = power
superhero = Superhero("Superman", ['X-rays vision', 'superhuman strength', 'super-speed'])
print(superhero.power.__class__.__name__)
print(superhero.power.__class__.__qualname__)
Output:
Power
Superhero.Power
Conclusion
This tutorial explained you the different methods to get the name or type of a class. Thank you for taking the time to read it and I hope it helped you.
Comments
Leave a comment