{"id":5090,"date":"2020-04-24T17:15:44","date_gmt":"2020-04-24T17:15:44","guid":{"rendered":"https:\/\/www.askpython.com\/?p=5090"},"modified":"2022-08-06T13:28:38","modified_gmt":"2022-08-06T13:28:38","slug":"python-type-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-type-function","title":{"rendered":"Variants of Python type() function"},"content":{"rendered":"\n<p>Hey, folks! In this article, we will having a look at one of the important built-in function of Python in terms of <em>debugging<\/em> &#8212; <strong>Python type() function<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started with Python type() function<\/h2>\n\n\n\n<p>Python type() function serves the purpose of debugging through-out the program. The type() function can be used to debug the data type of various classes and data variables throughout the code.<\/p>\n\n\n\n<p>The type() function can be represented in two variants&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>The type() function with one argument<\/strong><\/li><li><strong>The type() function with three arguments<\/strong><\/li><\/ul>\n\n\n\n<p>In the upcoming section, we will understand the functioning of both the variants of type() function in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Python type() with One Argument<\/h2>\n\n\n\n<p>When a single argument is passed to the type() function, it returns the data type of the given class\/object, respectively.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntype(object)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>It accepts only a <strong>single argument<\/strong>.<\/li><li>The type() function with single parameter returns the <strong>class type of the object <\/strong>passed to it.<\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndict_map = {&quot;Python&quot;:&#039;A&#039;,&quot;Java&quot;:&#039;B&#039;,&quot;Kotlin&quot;:&#039;C&#039;,&quot;Ruby&quot;:&#039;D&#039;}\nprint(&quot;The variable dict_map is of type:&quot;,type(dict_map))\n\nlist_inp = &#x5B;10,20,30,40]\nprint(&quot;The variable list_inp is of type:&quot;,type(list_inp))\n\nstr_inp = &quot;Python with JournalDev&quot;\nprint(&quot;The variable str_inp is of type:&quot;,type(str_inp))\n\ntup_inp = (&#039;Bajaj&#039;, &#039;Tata&#039;,&#039;Royce&#039;)\nprint(&quot;The variable tup_inp is of type:&quot;,type(tup_inp))\n\n<\/pre><\/div>\n\n\n<p>In the above example, we have created data objects of different data structures such as dict, list, etc. Further, we have passed it to the type() function to debug the type of the objects.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nThe variable dict_map is of type: &lt;class &#039;dict&#039;&gt;\nThe variable list_inp is of type: &lt;class &#039;list&#039;&gt;\nThe variable str_inp is of type: &lt;class &#039;str&#039;&gt;\nThe variable tup_inp is of type: &lt;class &#039;tuple&#039;&gt;\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Python type() with Three Arguments<\/h2>\n\n\n\n<p>When three parameters are passed to the <code>type() function<\/code>, it creates and returns a new type of object as output to the function.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntype(name, bases, dict)\n<\/pre><\/div>\n\n\n<p>The three parameters are as follows&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>name<\/code>: It is a <strong>string <\/strong>which basically represents the <strong>name of the class<\/strong>. <\/li><li><code>bases<\/code>: It is a <strong>tuple <\/strong>that specifies the <strong>base classes of the main class<\/strong>.<\/li><li><code>dict<\/code>: It is a &#8216;<strong>dictionary<\/strong>&#8216; that is used to <strong>create body of the class<\/strong> specified.<\/li><\/ul>\n\n\n\n<p>Thus, the type() function with the above three parameters are used to <strong>create classes dynamically<\/strong> at runtime.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nvar1 = type(&#039;ob&#039;, (object,), dict(A=&#039;Python&#039;, B=&#039;Cpp&#039;))\n\nprint(type(var1))\nprint(vars(var1))\n\nclass apply:\n  A = &#039;Python&#039;\n  B = &#039;Cpp&#039;\n  \nvar2 = type(&#039;oc&#039;, (apply,), dict(A = &#039;Python&#039;, B = &#039;Kotlin&#039;))\nprint(type(var2))\nprint(vars(var2))\n<\/pre><\/div>\n\n\n<p>In the above example, we have created classes at dynamic runtime one with a single object class and the other class with the &#8216;apply&#8217; base class to it. The<code> vars() function<\/code> represents the __dict__ argument of a class\/module.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;class &#039;type&#039;&gt;\n{&#039;A&#039;: &#039;Python&#039;, &#039;B&#039;: &#039;Cpp&#039;, &#039;__module__&#039;: &#039;__main__&#039;, &#039;__dict__&#039;: &lt;attribute &#039;__dict__&#039; of &#039;ob&#039; objects&gt;, &#039;__weakref__&#039;: &lt;attribute &#039;__weakref__&#039; of &#039;ob&#039; objects&gt;, &#039;__doc__&#039;: None}\n&lt;class &#039;type&#039;&gt;\n{&#039;A&#039;: &#039;Python&#039;, &#039;B&#039;: &#039;Kotlin&#039;, &#039;__module__&#039;: &#039;__main__&#039;, &#039;__doc__&#039;: None}\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The type() function with single parameter returns the class type of the parameter and is extensively used in debugging the code.<\/li><li>The type() function along with three parameters is used to create classes dynamically i.e. at run-time.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the working of Python type() under different parameters, respectively.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python type() function &#8212; JournalDev<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey, folks! In this article, we will having a look at one of the important built-in function of Python in terms of debugging &#8212; Python type() function. Getting started with Python type() function Python type() function serves the purpose of debugging through-out the program. The type() function can be used to debug the data type [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":5166,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-5090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=5090"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5090\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/5166"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=5090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=5090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=5090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}