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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b6b408c5c6a028888681/] [crayon-63b6b408c5c71137625871/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b6b408c5e5f879977162/] [crayon-63b6b408c5e63728914935/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63b6b408c5fbb194070616/] [crayon-63b6b408c5fbf996570019/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.