{"id":12980,"date":"2025-07-31T07:57:01","date_gmt":"2025-07-31T07:57:01","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12980"},"modified":"2025-07-31T07:57:01","modified_gmt":"2025-07-31T07:57:01","slug":"canonical-way-to-check-type","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/canonical-way-to-check-type","title":{"rendered":"Canonical Way to Check for Type in Python"},"content":{"rendered":"<p>Use isinstance() to check if an object is of a specific type or a subclass of it.<br \/>\n<strong>Example:<\/strong> Check if an object is of type str<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nmystr = \"hello\"\r\nif isinstance(mystr, str):\r\n   print(\"mystr is a string\")\r\n<\/pre>\n<p>This will return true even if mystr is an instance of a subclass of str.<br \/>\nTo check for an exact type (not allowing subclasses), use type():<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nif type(mystr) is str:\r\n   print(\"mystr is exactly a string\")\r\n<\/pre>\n<p>Use isinstance() in most cases, especially when you want to allow subclasses. It is more flexible and considered more Pythonic.<\/p>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/python-for-app-development\" target=\"_blank\">Python for App Development<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use isinstance() to check if an object is of a specific type or a subclass of it. Example: Check if an object is of type str mystr = &#8220;hello&#8221; if isinstance(mystr, str): print(&#8220;mystr is a string&#8221;) This will return true even if mystr is an instance of a subclass of str. To check for an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12981,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-12980","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12980"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12980"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12980\/revisions"}],"predecessor-version":[{"id":12982,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12980\/revisions\/12982"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12981"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}