Writing Versatile Python Scripts and Modules | __name__ and __main_

<p>In Python, the&nbsp;<code>__name__</code>&nbsp;variable is a special built-in variable that is used to determine whether a Python script is being run as the main program or if it is being imported as a module into another script. It allows us to write code that can be used both as a standalone script and as a module that can be imported into other scripts.</p> <p>When a Python script is executed, Python sets the&nbsp;<code>__name__</code>&nbsp;variable to&nbsp;<code>&quot;__main__&quot;</code>&nbsp;if the script is the entry point for the program. If the script is being imported as a module into another script, then the&nbsp;<code>__name__</code>&nbsp;variable is set to the&nbsp;<strong>name of the module</strong>.</p> <h1>Understanding the Problem</h1> <p>Imagine we&rsquo;re writing a Python script to do a specific task, like converting temperatures from Celsius to Fahrenheit.</p> <p>We want this script to work on its own when you run it as the main program. It should read the input from user, perform conversion and display the final result.</p> <p>But we also want to reuse the temperature conversion function in other programs we&rsquo;ll create in the future. But when importing this module in other modules, it should only perform the conversion. The input and output part will be handled by the calling program.</p> <p>So, how do you make a script that can wear both hats: a standalone program and a reusable module? That&rsquo;s where&nbsp;<code>__name__</code>&nbsp;comes in.</p> <p><a href="https://medium.com/@ccrrizal/writing-versatile-python-scripts-and-modules-name-and-main-10d12d7626e2">Click Here</a></p>