Writing Versatile Python Scripts and Modules | __name__ and __main_
<p>In Python, the <code>__name__</code> 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 <code>__name__</code> variable to <code>"__main__"</code> if the script is the entry point for the program. If the script is being imported as a module into another script, then the <code>__name__</code> variable is set to the <strong>name of the module</strong>.</p>
<h1>Understanding the Problem</h1>
<p>Imagine we’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’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’s where <code>__name__</code> comes in.</p>
<p><a href="https://medium.com/@ccrrizal/writing-versatile-python-scripts-and-modules-name-and-main-10d12d7626e2">Click Here</a></p>