{"id":18336,"date":"2021-11-17T15:55:35","date_gmt":"2021-11-17T10:25:35","guid":{"rendered":"https:\/\/java2blog.com\/?p=18336"},"modified":"2021-11-17T15:55:38","modified_gmt":"2021-11-17T10:25:38","slug":"get-class-name-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/get-class-name-python\/","title":{"rendered":"Get class name in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#How_to_get_the_attributes_of_an_object\">How to get the attributes of an object?<\/a><\/li><li><a href=\"#Get_Class_name_in_python_using_the___class___attributenbsp\">Get Class name in python using the __class__ attribute&nbsp;<\/a><\/li><li><a href=\"#Get_Class_name_in_python_using_type_function\">Get Class name in python using type() function<\/a><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n\n<p>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.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"How_to_get_the_attributes_of_an_object\">How to get the attributes of an object?<\/span><\/h2>\n\n\n\n<p>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, <code>Brand<\/code>, <code>Model<\/code>, <code>Price<\/code>, and <code>Country<\/code> are attributes of the <code>myCar<\/code> object created in the following example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n    def __init__(self, brand, model, price, country):\n        self.Brand = brand\n        self.Model = model\n        self.Price = price\n        self.Country = country\n<\/code><\/pre>\n\n\n\n<p>We can get other attributes associated with an object using the <code>dir() <\/code>function. The <code>dir()<\/code> function takes the object as its input argument and returns all the attributes of the object. You can observe this in the following example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n    def __init__(self, brand, model, price, country):\n        self.Brand = brand\n        self.Model = model\n        self.Price = price\n        self.Country = country\n\n\nmyCar = Car(\"Tesla\", \"Cybertruck\", 40000, \"USA\")\nprint(\"The attributes of the Car are:\")\nprint(dir(myCar))<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The attributes of the Car are:\n&#91;'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__']<\/code><\/pre>\n\n\n\n<p>Here, you can observe that we have an attribute named <code>__class__<\/code> in attributes of  <code>myCar<\/code>. This attribute of the object returns the class of the object.  We can use this attribute to get the class name in python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"Get_Class_name_in_python_using_the___class___attributenbsp\">Get Class name in python using the __class__ attribute&nbsp;<\/span><\/h2>\n\n\n\n<p>To get the class name of an object using the __class__ attribute, we will first obtain the class of the object as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n    def __init__(self, brand, model, price, country):\n        self.Brand = brand\n        self.Model = model\n        self.Price = price\n        self.Country = country\n\n\nmyCar = Car(\"Tesla\", \"Cybertruck\", 40000, \"USA\")\nmyClass = myCar.__class__\nprint(\"Class of myCar is:\", myClass)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class of myCar is: &lt;class '__main__.Car'&gt;<\/code><\/pre>\n\n\n\n<p>Here, you can observe that we have got output in the form of <code>&lt;class '__main__.Car'&gt;<\/code>. This output shows that the class <code>Car<\/code> has been defined in the scope of the <code>__main__<\/code> module i.e. the outermost scope. We can get the class name from this output using the <code>__name__<\/code> attribute as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n    def __init__(self, brand, model, price, country):\n        self.Brand = brand\n        self.Model = model\n        self.Price = price\n        self.Country = country\n\n\nmyCar = Car(\"Tesla\", \"Cybertruck\", 40000, \"USA\")\nmyClass = myCar.__class__\nprint(\"Class of myCar is:\", myClass)\nprint(\"Name of the class is:\", myClass.__name__)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class of myCar is: &lt;class '__main__.Car'&gt;\nName of the class is: Car<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"Get_Class_name_in_python_using_type_function\">Get Class name in python using type() function<\/span><\/h2>\n\n\n\n<p>The <code>type()<\/code> 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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n    def __init__(self, brand, model, price, country):\n        self.Brand = brand\n        self.Model = model\n        self.Price = price\n        self.Country = country\n\n\nmyCar = Car(\"Tesla\", \"Cybertruck\", 40000, \"USA\")\nmyClass = type(myCar)\nprint(\"Class of myCar is:\", myClass)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class of myCar is: &lt;class '__main__.Car'&gt;<\/code><\/pre>\n\n\n\n<p>Here, you can again observe that the output is in the form <code>&lt;class '__main__.Car'&gt;<\/code> showing that the class <code>Car<\/code> has been defined in the scope of the <code>__main__<\/code> module. Again, we can get the class name using the <code>__name__<\/code> attribute from the output of the <code>type()<\/code> function as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car:\n    def __init__(self, brand, model, price, country):\n        self.Brand = brand\n        self.Model = model\n        self.Price = price\n        self.Country = country\n\n\nmyCar = Car(\"Tesla\", \"Cybertruck\", 40000, \"USA\")\nmyClass = type(myCar)\nprint(\"Class of myCar is:\", myClass)\nprint(\"Name of the class is:\", myClass.__name__)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class of myCar is: &lt;class '__main__.Car'&gt;\nName of the class is: Car<\/code><\/pre>\n\n\n\n<p>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<code> __class__<\/code> attribute discussed in the previous section.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n\n\n\n<p>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 <code>__class__<\/code> attribute instead of the <code>type()<\/code> function will make your code more portable as it can be run on older as well as newer versions of python.<\/p>\n\n\n<p>THat&#8217;s all about how to get class name in Python.<\/p>\n\n\n<p>I hope you enjoyed reading this article. Stay tuned for more such informative articles.<\/p>\n\n\n\n<p>Happy Learning.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute&nbsp;Get Class name in python using type() functionConclusion 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 [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":18389,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18336"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=18336"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18336\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/18389"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=18336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=18336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=18336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}