Categories: python

Python __name__ Variable

Because Python lacks a main() function, when the command to run a Python program is given to the interpreter, the code at level 0 indentation is executed. However, it will first define a few special variables. One such special variable is __name__. If the source file is run as the main program, the interpreter assigns the value “main” to the __name__ variable. If this file is imported from another module, __name__ will be set to the name of the module.

What is __name__ Variable?
  • In Python, the __name__ variable is a special built-in variable that contains the name of the current module.
  • It is automatically set when a module is imported or run as the main program.
  • The value __name__ is set to the string "__main__" when a Python script is run directly (for example, by running python myscript.py from the command line). This allows you to use the if __name__ == "__main__" idiom to determine whether a module is being run as the main program or being imported as a module.

For example, consider the following script myscript.py:

def main():
    print("This is the main function.")

if __name__ == "__main__":
    main()

If you run this script directly, by running python myscript.py, it will output “This is the main function.” However, If you import this script as a module in another script, the if-statement will not be executed and the main() function will not be called.

import myscript

This can be useful for running certain code only when the module is run directly, but not when it is imported as a module. It also can be used to organize the code in the way that while importing the module it will only import the functionalities, methods, and classes it will not execute the code which is in the if name == “main” block.

Using if __name__ == __main__:

Using if __name__ == "__main__" is a common idiom in Python to determine whether a module is being run as the main program or being imported as a module. It allows you to separate the code that should only be executed when the module is run directly from the code that should always be available when the module is imported.

Here is an example that demonstrates the usage of if __name__ == "__main__" idiom :

def my_func():
    print("This function is available when the module is imported.")

if __name__ == "__main__":
    print("This code will only be run when the module is executed directly.")
    my_func()

Output:

This code will only be run when the module is executed directly.
This function is available when the module is imported.

However, if you import this module into another script, the code inside the if block will not be executed. Only my_func() will be available for use in the other script.

This approach allows you to write and test the main() or any other function in your script separately from the functions which you want to import.

__name__: Printing it’s Value

You can print the value of the __name__ variable to see what it is set to. The value of __name__ will be different depending on whether the module is being run directly or imported as a module.

Example:

# mymodule.py
def my_func():
    print("This function is always available when the module is imported.")

print("The value of __name__ is:", __name__)

Output:

The value of __name__ is: __main__

Note: also read about Barrier Object in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago