{"id":4519,"date":"2022-12-28T10:07:52","date_gmt":"2022-12-28T10:07:52","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4519"},"modified":"2022-12-28T10:07:54","modified_gmt":"2022-12-28T10:07:54","slug":"name-variable-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/name-variable-in-python\/","title":{"rendered":"What is name variable in Python?"},"content":{"rendered":"\n<p>In many different python programs, you might have seen the statement <strong>if __name__ == &#8220;__main__&#8221;<\/strong>. So here in this article, we are going to understand a special variable <strong>__name__<\/strong>. We would see some different examples, through which, we can understand what is __name__ variable, and this would also give us some idea about the instruction <strong>if __name__ == &#8220;__main__&#8221;, <\/strong>which we use in many times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is name variable in Python?<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/name-variable-in-Python.jpg\" alt=\"\" class=\"wp-image-4521\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/name-variable-in-Python.jpg 640w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/12\/name-variable-in-Python-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n<\/div>\n\n\n<p>The <strong>__name__<\/strong> variable in Python is a special variable. The value for the __name__ variable depends on how are we executing the containing script. At times, it is useful to make use of the __name__ variable, as you might have seen sometimes, in the statement <strong>if __name__ == &#8220;__main__&#8221;<\/strong>.<\/p>\n\n\n\n<p>The question now is that what are the values contained by the special variable __name__. So now, let&#8217;s have a look at different conditions, in which, we can have different values for the special variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What values can the variable __name__ have?<\/h3>\n\n\n\n<p>The thing is that when the script is executed directly, the <strong>__name__<\/strong> variable is set to &#8216;__main__&#8217;. On the other hand, if the file is imported as the module, then the __name__ variable is set to the name of the module. To understand this more clearly, let&#8217;s have a look at an example, which gives us a clearer idea about the scenarios.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Case 1 \u2013 When our python file is executed directly.<\/h4>\n\n\n\n<p>As mentioned earlier, when the python file is executed directly, the __name__ variable is set to &#8216;__main__&#8217;. So, let&#8217;s have a look at a simple example, which tries to demonstrate the same thing.<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">def dosomething():<br>&nbsp; &nbsp; print(&#8220;The value in the __name__ variable is: &#8221; + __name__)<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">dosomething()<\/p>\n\n\n\n<p>As you can see, in the above program, we just have a simple function, with the name do something, in which, we are printing the value of the __name__ variable. After that, we are just calling the do something function.<\/p>\n\n\n\n<p>Let&#8217;s say that the name of this program is gyanipandit.py, and we are executing this program directly, then the __name__ variable is set to &#8216;__main__&#8217;. You can try to execute the program. So now, let&#8217;s have a look at the output from the above program.<\/p>\n\n\n\n<p><strong>Output &#8211;&nbsp;<\/strong><\/p>\n\n\n\n<p>The value in the __name__ variable is: __main__<\/p>\n\n\n\n<p>As you can see, since we are executing the file directly, and not importing it, the __name__ variable is set to &#8216;__main__&#8217;.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Case 2 \u2013 When we import our python file into another file &#8211;<\/h4>\n\n\n\n<p>In the prior case, we saw that if we are executing the file directly, then the __name__ variable is set to &#8216;__main__&#8217;. But in this case, let&#8217;s consider that we are not executing the file directly, but importing it into another python file. In such a case, the __name__ variable is set to the name of the module. Let&#8217;s have a look at a simple program, through which, we can understand the same thing &#8211;<\/p>\n\n\n\n<p>Let&#8217;s say that we have a program, with the name program.py. Here is the code for program.py &#8211;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">def dosomething():<br>&nbsp; &nbsp; print(&#8220;The value in the __name__ variable is: &#8221; + __name__)<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">if __name__ == &#8216;__main__&#8217;:<br>\u00a0 \u00a0 dosomething()<\/p>\n\n\n\n<p>On the other hand, let&#8217;s say that we have another file gyanipandit.py, in which, we are importing our program file. Here is the code for the same.<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">import program<br>print(&#8220;We have imported the python file into another python file&#8221;)<br>program.dosomething()<\/p>\n\n\n\n<p>As you can see in the above program, we have imported the program file. Then we are calling that do something function, and if we try to execute the file gyanipandit.py, you can find the output as something like this &#8211;<\/p>\n\n\n\n<p><strong>Output &#8211;&nbsp;<\/strong><\/p>\n\n\n\n<p>We have imported the python file into another python file<br>The value in the __name__ variable is: program<\/p>\n\n\n\n<p>This last line of output comes from the program module. Since we are calling the do something method, in which, we are printing the value for the __name__ variable, and now, we find that since the program file is not executed directly, but imported into another python file, the __name__ variable was set to the module name.<\/p>\n\n\n\n<p>Also, you can observe that in the code for the program.py file, we have used the statement <strong>if __name__ == &#8216;__main__&#8217;<\/strong>. With the help of this statement, we are simply saying that if this file is directly being executed, then only do the other stuff. This helps us when we want to import the file into another file, since on importing, we might not want to directly execute the things from the imported module.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion &#8211; <\/h4>\n\n\n\n<p>Here, we have seen the special variable __name__, We now know that the variable has a value &#8216;__main__&#8217;, if we are executing the file directly, and if the file is being imported, the __name__ variable is set to the module name. The __name__ variable is very useful at times.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FAQ About name variable in Python<\/h4>\n\n\n\n<div class=\"wp-block-rank-math-faq-block\"><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What is a <strong>name<\/strong> variable in Python?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>The <strong>name<\/strong> variable is a special variable, which is set to &#8216;<strong>main<\/strong>&#8216; if the file is executed directly. On the other hand, if the file is imported, then the <strong>name<\/strong> variable is set to the module name.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: what is if <strong>name<\/strong> == &#8216;<strong>main<\/strong>&#8216; in Python?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>In our python programs, we often use the if <strong>name<\/strong> == &#8216;<strong>main<\/strong>&#8216; statement. By doing this, we simply mean that do the further things only if the file is directly executed, and not imported.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What is the use of <strong>name<\/strong> variables in python?<\/h3><div class=\"rank-math-answer\"><strong>Ans: The name variable is a special variable that<\/strong> is set to &#8216;<strong>main<\/strong>&#8216;, if the file is directly executed. But if we are importing the file, then the <strong>name<\/strong> variable is set to the module name.<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In many different python programs, you might have seen the statement if __name__ == &#8220;__main__&#8221;. So here in this article, we are going to understand a special variable __name__. We would see some different examples, through which, we can understand what is __name__ variable, and this would also give us some idea about the instruction [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4521,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[26,93],"class_list":{"0":"post-4519","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"tag-python","9":"tag-what-is-name-variable-in-python"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4519"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4519\/revisions"}],"predecessor-version":[{"id":4522,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4519\/revisions\/4522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media\/4521"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}