Get class name in Python

Get class name in Python

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.

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.

Output:

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.

Output:

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.

Output:

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.

Output:

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.

Output:

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.


import_contacts

You may also like:

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Return vs Print in Python
    16 April

    Return vs Print in Python

    Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

  • Floor division in Python
    10 April

    Floor division in Python

    Table of ContentsWhat is Floor Division in Python?How to implement floor division in Python?Using the // operator to implement floor division in Python.Using the // operator to implement floor division on negative integers in Python.Using the // operator to implement floor division on floating-point numbers in Python.Using the // operator to implement floor division with […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.