Table of Contents
While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an object in python.
How to get the attributes of an object?
An object in python has many attributes such as variables defined in its class definition, methods, and attributes containing metadata about the object. For instance, Brand, Model, Price, and Country are attributes of the myCar object created in the following example.
|
1 2 3 4 5 6 7 8 9 |
class Car: def __init__(self, brand, model, price, country): self.Brand = brand self.Model = model self.Price = price self.Country = country |
We can get other attributes associated with an object using the dir() function. The dir() function takes the object as its input argument and returns all the attributes of the object. You can observe this in the following example.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Car: def __init__(self, brand, model, price, country): self.Brand = brand self.Model = model self.Price = price self.Country = country myCar = Car("Tesla", "Cybertruck", 40000, "USA") print("The attributes of the Car are:") print(dir(myCar)) |
Output:
|
1 2 3 4 |
The attributes of the Car are: ['Brand', 'Country', 'Model', 'Price', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] |
Here, you can observe that we have an attribute named __class__ in attributes of myCar. This attribute of the object returns the class of the object. We can use this attribute to get the class name in python.
Get Class name in python using the __class__ attribute
To get the class name of an object using the __class__ attribute, we will first obtain the class of the object as follows.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Car: def __init__(self, brand, model, price, country): self.Brand = brand self.Model = model self.Price = price self.Country = country myCar = Car("Tesla", "Cybertruck", 40000, "USA") myClass = myCar.__class__ print("Class of myCar is:", myClass) |
Output:
|
1 2 3 |
Class of myCar is: <class '__main__.Car'> |
Here, you can observe that we have got output in the form of <class '__main__.Car'>. This output shows that the class Car has been defined in the scope of the __main__ module i.e. the outermost scope. We can get the class name from this output using the __name__ attribute as follows.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Car: def __init__(self, brand, model, price, country): self.Brand = brand self.Model = model self.Price = price self.Country = country myCar = Car("Tesla", "Cybertruck", 40000, "USA") myClass = myCar.__class__ print("Class of myCar is:", myClass) print("Name of the class is:", myClass.__name__) |
Output:
|
1 2 3 4 |
Class of myCar is: <class '__main__.Car'> Name of the class is: Car |
Get Class name in python using type() function
The type() function is used to determine the class to which an object belongs in python. The type() function takes an object as an input argument and returns the class of the object in the following format.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Car: def __init__(self, brand, model, price, country): self.Brand = brand self.Model = model self.Price = price self.Country = country myCar = Car("Tesla", "Cybertruck", 40000, "USA") myClass = type(myCar) print("Class of myCar is:", myClass) |
Output:
|
1 2 3 |
Class of myCar is: <class '__main__.Car'> |
Here, you can again observe that the output is in the form <class '__main__.Car'> showing that the class Car has been defined in the scope of the __main__ module. Again, we can get the class name using the __name__ attribute from the output of the type() function as follows.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Car: def __init__(self, brand, model, price, country): self.Brand = brand self.Model = model self.Price = price self.Country = country myCar = Car("Tesla", "Cybertruck", 40000, "USA") myClass = type(myCar) print("Class of myCar is:", myClass) print("Name of the class is:", myClass.__name__) |
Output:
|
1 2 3 4 |
Class of myCar is: <class '__main__.Car'> Name of the class is: Car |
Please be careful that this approach might not work for you if you are working with python 2.x. To get the class name in such a situation, you should use the __class__ attribute discussed in the previous section.
Conclusion
In this article, we have discussed two ways to get the class name in python. You may choose any of the approaches but choosing the __class__ attribute instead of the type() function will make your code more portable as it can be run on older as well as newer versions of python.
THat’s all about how to get class name in Python.
I hope you enjoyed reading this article. Stay tuned for more such informative articles.
Happy Learning.