The word __name__ in Python represents a special variable
Let's find out how you can use it in your Python programs
There are many special variables in Python that start and end with double underscores
To make it short, they are referred to as dandas from double underscores
So __name__ is pronounced dander name. Let's see what the value of dander name is by using the Python shell
As you can see, the value is __name__. Now, let's try to import a Python module to see the value assigned to the dander name variable associated to the module
So, after importing the random module, we can see that the value of dander name for the module is random
It's basically the name of the module. So, we know a little bit more than before, but let's see if we can clarify the concept using a Python program
Create a file called understand__name__py and this file will contain the following code
Our code contains a single print statement that uses the string format method to print the value of dander name
Let's see what happens when we run this code directly. When we execute this code directly by referencing the Python file, the value of dander name is dander name
Now, let's open the Python shell and import the module we have created
So, this time the value of dander name is understand__name__name. It's basically the name of our Python module
The behavior is exactly the same we have seen before when we imported the random module
But why the value of dander name is different depending on the way we run our Python code
How can we use this in our Python program? To explain this concept, we will create a Python program that calls three different functions
We will use the file .py that we have created before. We will use the file .py that we have created before
We will use the file .py that we have created before. This code runs three functions
Three very simple functions that call a print statement. When we run our code, we see the output of the three functions
Now, let's say another Python program needs to use the function step1
To do that, we would have to import our file as a module. Here is what happens when we do that
After importing the module, we can execute the function step1. The problem is that when we import the module
the three functions step1, step2, and step3 are executed automatically. To prevent this behavior, we can check if the value of dunder names is equal to dunder names
In this way, the code inside each condition is executed only if the Python file is called directly
and is not executed when it is imported as a module. Let's find out how our program becomes
We just need to add an extra if statement. Let's confirm that the three functions are still executed when we call the Python program directly
All good so far. Now, we want to confirm that the three functions are not executed when we import this as a module
That's good. The if statement we've added to our Python program prevents the three functions
from being executed automatically when we import our module. And let's confirm that we can still call the step1 function after importing the module
Also, for a moment, assume that the new module we have created includes hundreds of functions
and you don't want to import all of them. How can you just import the step1 function
To do that, we can use the following syntax. from name of the module import
the function you want to import. In this example, step1. As you can see, we can call the step1 function without problems
Let's see what happens when we call step2. The Python interpreter tells us that step2 is not defined because we have only imported step1
Also, for those who are not familiar, I want to introduce the concept of main
This is quite standard in languages like Java or C, and it represents the entry point for the execution of a program
A common approach used in Python is to create a function called main
executed inside the if statement that checks the value of the dundered main variable
The main function is the place where all our functions are called to obtain a specific result
In our case, in our main function, we will call the functions step1, step2, and step3
Main in the function main is just a common name convention
It's something that you can use to increase the readability of your code because it's a concept well known to other developers
In reality, you could use any name you want for the main function
Before continuing to verify that this code works well, both by executing the code directly and also by importing this module
Before completing this tutorial, I want to show you how to pass arguments to your Python application
To handle them, we will use the sys module. Let's create a file called test-args.py
First of all, we import the sys module. Then we will define the main function that takes as input args and prints the value of args
We will understand what args is when we execute our program. And finally, we define an if statement to verify when the value of dundered main is equal to dundered main
Inside the if statement, we call the main function and we pass to it sys.argv
Let's see what happens when we run our program in the terminal
The first argument received by the Python program is the name of the file .py itself, test-args.py
Let's see what happens if we pass two arguments, arg1 and arg2
This time our program prints a bigger list. The first item of the list is still the name of the Python program
The second item is arg1 and the third item is arg2. Let's update our program to print the type of the args variable
As you can see, the args variable is of type list. When dealing with arguments in a Python program
we can also unpack the arguments we need in our program and exclude the name of the program itself
For example, let's create a program that calculates the product of two numbers
As you can see, we use the slice operator to get the values of arg1 and arg2
We then pass arg1 and arg2 to the main function that returns the product of the two numbers
Let's verify that the program works as expected. Now you know how to use dunder name in your Python programs
And you also know what are the different values that dunder name gets depending on how you run the Python program
either if you run it directly or if you import it as a module
If you found this video useful, give it a like. Also, remember to subscribe to Codefather
so you don't miss any of the tutorials that I will be publishing in the future
Happy coding and see you in the next video