__name__ Variables

__name__ Variable

Dunder

In Python, we have what we call ‘dunder’ names, and it refers to a naming convention that uses double underscores at the start and end of the variable or function. The term ‘dunder’ means double underscores.

The term ‘__name__’ is a special variable in Python which refers to the current script that is being run. When a script is being run, the variable ‘__name__’ is assigned the value ‘__main__’, and any other script such as an import module will have the name of the module itself.

Let’s create a python script called ‘dunder_.py’ and write the following code in it.

def print_name():
    print("The '__name__' variable holds the value:", __name__)

print_name()

Code from dunder_.py

When we run this, we should get:

Now let’s create another script called ‘dunder_main_.py’ which imports our code from the above script.

import dunder_

# __name__ from dunder_main_.py
print("dunder_main_.py")
print("The '__name__' variable in dunder_main_.py has:", __name__)

# __name__ from dunder_.py
print("\ndunder_.py")
dunder_.print_name()

Code from dunder_main_.py

When we run this, we can see the difference in the __name__ variable.

Here the main script is the ‘dunder_main_.py’ and the imported script is the ‘dunder_.py’ and the code is run using the main script and we call a function from the imported script. Thus Python would assign ‘__main__’ for the ‘__name__’ variable in the main script and would assign the module name for the ‘__name__’ variable in the imported script.

If Main

__name__ provides us a useful feature of checking whether or not a script is being run directly or if it is being imported and to do some action accordingly.

# Name of the script
print("dunder_.py")

if __name__ == "__main__":
    # Yes, it is the main script
    print("From Main; __name__ is:", __name__)
else:
    # No, it is being imported
    print("This script is imported and its name is __name__ is:", __name__)

Code from dunder_.py

import dunder_




# Name of the script
print("\ndunder_main_.py")

if __name__ == "__main__":
    # Yes, it is the main script
    print("From Main; __name__ is:", __name__)
else:
    # No, it is being imported
    print("This script is imported and its name is __name__ is:", __name__)

Code from dunder_main_.py

Here we have an if statement to check if the code being executed is from the main script or not. If it is the main script, we want it to print a statement that indicates that this is the main script by displaying ‘__main__’, and if it is not the main script or if it’s imported it should print the module name.

When we run this, we get:

Once again, here we see that since we ran the script from dunder_main_.py its __name__ variable will be assigned the value ‘__main__’ and will execute its if statement accordingly. Also, since we imported dunder_.py it will execute the else statement of its if condition.

What have we learned?

  • What does the term dunder mean?
  • What is the naming convention of a dunder?
  • What is the purpose of using the __name__ dunder?
  • How to use __name__ with the if statement?
  • What is the default value assigned to __name__ if it is not the main script?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments