{"id":1320,"date":"2023-12-23T00:00:00","date_gmt":"2023-12-23T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1320"},"modified":"2023-12-12T05:12:15","modified_gmt":"2023-12-12T10:12:15","slug":"python-what-does-if-__name__-__main__-do","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python-what-does-if-__name__-__main__-do\/","title":{"rendered":"Python: What does if __name__ == &#8220;__main__&#8221;: do?"},"content":{"rendered":"\n<p>The <code>if __name__ == \"__main__\":<\/code> statement in Python is a common idiom used to determine if the Python script is being run as the main program or if it is being imported as a module into another script. Let&#8217;s break down its purpose and functionality:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if __name__ == \"__main__\":\r\n    # Code to execute when the script is run as the main program\r<\/code><\/pre>\n\n\n\n<p>Here&#8217;s what happens:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>__name__<\/code> Variable:<\/strong>\n<ul class=\"wp-block-list\">\n<li>In Python, the <code>__name__<\/code> variable is a special variable that is automatically set by the Python interpreter.<\/li>\n\n\n\n<li>When a Python script is executed, <code>__name__<\/code> is set to <code>\"__main__\"<\/code> if the script is the main program being run.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Program Execution:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The <code>if __name__ == \"__main__\":<\/code> statement checks if the script is being run as the main program.<\/li>\n\n\n\n<li>If true, the indented code block beneath it is executed.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Importing as a Module:<\/strong>\n<ul class=\"wp-block-list\">\n<li>When a Python script is imported as a module into another script, <code>__name__<\/code> is set to the name of the module, not <code>\"__main__\"<\/code>.<\/li>\n\n\n\n<li>In this case, the code block beneath <code>if __name__ == \"__main__\":<\/code> is not executed.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Use Cases:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Script Execution:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The code inside the <code>if __name__ == \"__main__\":<\/code> block is typically where you place the main functionality of your script that you want to execute when the script is run directly.<\/li>\n\n\n\n<li>This allows you to have reusable modules that can also be used as standalone scripts.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Avoiding Unintended Execution:<\/strong>\n<ul class=\"wp-block-list\">\n<li>It prevents the unintended execution of code when the script is imported as a module.<\/li>\n\n\n\n<li>Without this condition, any code outside the <code>if __name__ == \"__main__\":<\/code> block would be executed upon import.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Example module\/script\r\n\r\ndef some_function():\r\n    print(\"This function can be reused\")\r\n\r\nif __name__ == \"__main__\":\r\n    # Code to execute when the script is run as the main program\r\n    print(\"This script is being run directly.\")\r\n    some_function()\r<\/code><\/pre>\n\n\n\n<p>In this example, <code>some_function()<\/code> can be imported and used in another script without executing the code inside the <code>if __name__ == \"__main__\":<\/code> block. When the script is run directly, the block is executed, and the message is printed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The if __name__ == &#8220;__main__&#8221;: statement in Python is a common idiom used to determine if the Python script is being run as the main program or if it is being imported as a module into another script. Let&#8217;s break down its purpose and functionality: Here&#8217;s what happens: Use Cases: Example: In this example, some_function() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[],"class_list":["post-1320","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1320"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1320\/revisions"}],"predecessor-version":[{"id":1321,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1320\/revisions\/1321"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}