{"id":129,"date":"2024-01-25T11:30:24","date_gmt":"2024-01-25T11:30:24","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=129"},"modified":"2024-01-25T11:30:26","modified_gmt":"2024-01-25T11:30:26","slug":"python-__name__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__name__\/","title":{"rendered":"Python __name__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python&nbsp;<code>__name__<\/code>&nbsp;variable and how to use it effectively in modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What\u2019s Python&nbsp;<code>__name__<\/code>?<\/h2>\n\n\n\n<p>If you have gone through Python code, you\u2019ve likely seen the\u00a0<code>__name__<\/code>\u00a0variable like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if __name__ == '__main__': main()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And you might wonder what the&nbsp;<code>__name__<\/code>&nbsp;variable is.<\/p>\n\n\n\n<p>Since the&nbsp;<code>__name__<\/code>&nbsp;variable has double underscores at both sides, it\u2019s called&nbsp;<strong>dunder name<\/strong>. The dunder stands for&nbsp;<strong>d<\/strong>ouble&nbsp;<strong>under<\/strong>scores<\/p>\n\n\n\n<p>The&nbsp;<code>__name__<\/code>&nbsp;is a special variable in Python. It\u2019s special because Python assigns a different value to it depending on how its containing script executes.<\/p>\n\n\n\n<p>When you import a module, Python executes the file associated with the module.<\/p>\n\n\n\n<p>Often, you want to write a script that can be executed directly or imported as a module. The&nbsp;<code>__name__<\/code>&nbsp;variable allows you to do that.<\/p>\n\n\n\n<p>When you run the script directly, Python sets the&nbsp;<code>__name__<\/code>&nbsp;variable to&nbsp;<code>'__main__'<\/code>.<\/p>\n\n\n\n<p>However, if you import a file as a module, Python sets the module name to the&nbsp;<code>__name__<\/code>&nbsp;variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python&nbsp;<code>__name__<\/code>&nbsp;variable example<\/h2>\n\n\n\n<p>First, create a new module called\u00a0<code>billing<\/code>\u00a0that has two functions:\u00a0<code>calculate_tax()<\/code>\u00a0and\u00a0<code>print_billing_doc()<\/code>. In addition, add a statement that prints out the\u00a0<code>__name__<\/code>\u00a0variable to the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def calculate_tax(price, tax): return price * tax def print_billing_doc(): tax_rate = 0.1 products = &#91;{'name': 'Book', 'price': 30}, {'name': 'Pen', 'price': 5}] <em># print billing header<\/em> print(f'Name\\tPrice\\tTax') <em># print the billing item<\/em> for product in products: tax = calculate_tax(product&#91;'price'], tax_rate) print(f\"{product&#91;'name']}\\t{product&#91;'price']}\\t{tax}\") print(__name__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Second, create a new file called\u00a0<code>app.py<\/code>\u00a0and import the\u00a0<code>billing<\/code>\u00a0module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import billing<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you execute the\u00a0<code>app.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>> python app.py<\/code><small>Code language: CSS (css)<\/small><\/code><\/pre>\n\n\n\n<p>\u2026the\u00a0<code>__name__<\/code>\u00a0variable shows the following value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>billing<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>It means that Python does execute the&nbsp;<code>billing.py<\/code>&nbsp;file when you import the billing module to the&nbsp;<code>app.py<\/code>&nbsp;file.<\/p>\n\n\n\n<p>The&nbsp;<code>__name__<\/code>&nbsp;variable in the&nbsp;<code>app.py<\/code>&nbsp;set to the module name which is&nbsp;<code>billing<\/code>.<\/p>\n\n\n\n<p>If you execute the\u00a0<code>billing.py<\/code>\u00a0as a script directly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>> python billing.py<\/code><small>Code language: CSS (css)<\/small><\/code><\/pre>\n\n\n\n<p>\u2026 you\u2019ll see the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>__main__<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this case the value of the&nbsp;<code>__name__<\/code>&nbsp;variable is&nbsp;<code>'__main__'<\/code>&nbsp;inside the&nbsp;<code>billing.py<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-__name__.png\" alt=\"Python __name__\" class=\"wp-image-578\"\/><\/figure>\n\n\n\n<p>Therefore, the&nbsp;<code>__name__<\/code>&nbsp;variable allows you to check when the file is executed directly or imported as a module.<\/p>\n\n\n\n<p>For example, to execute the\u00a0<code>print_billing_doc()<\/code>\u00a0function when the\u00a0<code>billing.py<\/code>\u00a0executes directly as a script, you can add the following statement to the\u00a0<code>billing.py<\/code>\u00a0module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if __name__ == '__main__': print_billing_doc()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Third, execute the\u00a0<code>billing.py<\/code>\u00a0as a script, you\u2019ll see the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Name Price Tax Book 30 3.0 Pen 5 0.5<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>However, when you execute the&nbsp;<code>app.py<\/code>, you won\u2019t see the&nbsp;<code>if<\/code>&nbsp;block executed because the&nbsp;<code>__name__<\/code>&nbsp;variable doesn\u2019t set to the&nbsp;<code>'__main__'<\/code>&nbsp;but&nbsp;<code>'billing'<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python&nbsp;__name__&nbsp;variable and how to use it effectively in modules. What\u2019s Python&nbsp;__name__? If you have gone through Python code, you\u2019ve likely seen the\u00a0__name__\u00a0variable like the following: And you might wonder what the&nbsp;__name__&nbsp;variable is. Since the&nbsp;__name__&nbsp;variable has double underscores at both sides, it\u2019s called&nbsp;dunder name. The dunder stands for&nbsp;double&nbsp;underscores [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-9-modules-packages"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/129\/revisions\/130"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}