- What does __name__==’__main__’ mean in Python ?
- What does name==’main’ mean in Python ?
- Wrapping It Up
- What’s in a (Python’s) __name__?
- Why is the _ _name_ _ variable used?
- What values can the __name__ variable contain?
- Scenario 1 — Run the script
- Scenario 2 — Import the script in another script
- Conclusion
- What does “__name__” mean in Python?
- Let’s get straight to it.
- What values can __name__ take? How does it work?
- So what’s the purpose of __name__?
What does __name__==’__main__’ mean in Python ?
In this tutorial, you’ll learn what does name==’main’ mean in Python.
All python beginners, once in a while must have stumbled upon the below piece of code.
What does it actually mean and what is the use of the above piece of code. We’ll see what the above code does and its relevance with the help of an example.
What does name==’main’ mean in Python ?
Lets create a simple python program hello.py with a function called foo.
#!/usr/bin/python # hello.py def foo(): print 'foo is called' foo()
If you try running the above program, you should have the following output on the console.
Now, suppose we need to import this hello.py file into another python program in order to access the foo function.
Let’s create another python program called world.py. In world.py we’ll import our hello.py and try to call foo. Here is how_world.py_ would look like:
#!/usr/bin/python # world.py import hello def bar(): hello.foo() bar()
Now, if you execute world.py, you should have the following output on the terminal console;
foo is called foo is called
We’ll as you saw the message gets printed twice. The simple solution would be to remove the foo function call from_hello.py_. But in that case it won’t print the message when we execute hello.py. Well, this is where
# Our Hero ;) __name__=='__main__'
comes into the picture. When a python program starts to execute, it sets a few special variable, name is one of them. It contains the value main if it’s the main program being executed. But it won’t have the value as main being set, if it’s being imported and executed, since it’s not the main program when imported.
Let’s modify hello.py to include the checking for name .
#!/usr/bin/python # hello.py def foo(): print 'foo is called' if __name__ == '__main__': foo()
Now, if you try running the world.py program it won’t print the message twice. The only message that is printed is of the hello.foo function call.
Wrapping It Up
In this short tutorial, you learnt what does name==’main’ mean in Python. Hope the python examples were clear.
Do let us know your thoughts in the comments below.
What’s in a (Python’s) __name__?
You’ve most likely seen the __name__ variable when you’ve gone through Python code. Below you see an example code snippet of how it may look:
if __name__ == '__main__': main()
In this article, I want to show you how you can make use of this variable to create modules in Python.
Why is the _ _name_ _ variable used?
The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script.
Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.
Thanks to this special variable, you can decide whether you want to run the script. Or that you want to import the functions defined in the script.
What values can the __name__ variable contain?
When you run your script, the __name__ variable equals __main__ . When you import the containing script, it will contain the name of the script.
Let us take a look at these two use cases and describe the process with two illustrations.
Scenario 1 — Run the script
Suppose we wrote the script nameScript.py as follows:
def myFunction(): print 'The value of __name__ is ' + __name__
if __name__ == '__main__': main()
If you run nameScript.py, the process below is followed.
Before all other code is run, the __name__ variable is set to __main__. After that, the main and myFunction def statements are run. Because the condition evaluates to true, the main function is called. This, in turn, calls myFunction. This prints out the value of __main__ .
Scenario 2 — Import the script in another script
If we want to re-use myFunction in another script, for example importingScript.py , we can import nameScript.py as a module.
The code in importingScript.py could be as follows:
We then have two scopes: one of importingScript and the second scope of nameScript . In the illustration, you’ll see how it differs from the first use case.
In importingScript.py the __name__ variable is set to __main__. By importing nameScript, Python starts looking for a file by adding .py to the module name. It then runs the code contained in the imported file.
But this time it is set to nameScript. Again the def statements for main and myFunction are run. But, now the condition evaluates to false and main is not called.
In importingScript.py we call myFunction which outputs nameScript. NameScript is known to myFunction when that function was defined.
If you would print __name__ in the importingScript, this would output __main__ . The reason for this is that Python uses the value known in the scope of importingScript.
Conclusion
In this short article, I explained how you can use the __name__ variable to write modules. You can also run these modules on their own. This can be done by making use of how the values of these variables change depending on where they occur.
What does “__name__” mean in Python?
Python is such a popular language because it’s easy to use and it’s comprehensive. Previously, I covered what the keyword yield was useful for and with some great feedback, I have decided to tackle another key feature: __name__ .
Let’s get straight to it.
Now quite often you’ll see a file of the following format
def main():
# some functionality.
return library.function()
if __name__ == ‘__main__’:
main()
At first, I struggled to understand why the file didn’t compile in one straight line but after learning about it, I began to appreciate it’s simplicity.
It comes down to a few things:
- You have some functions that people can use elsewhere
- You also want to run that file by itself at times
So it’s a bit of a quasi-library type of file. To appreciate it fully, first you should understand what values __name__ can take.
What values can __name__ take? How does it work?
Let’s say that we have a file called foo.py and the only thing in the file is the following:
Now if we run the following line on the command line:
We will get the following result:
But on the other hand, say we have another file bar.py that contains:
import foo
print(foo.__name__)
And if you run python bar.py you will get the following output:
If you run the file itself, then __name__ == ‘__main__’ but if you call __name__ from another file, then the name of the file is returned.
So __name__ gets its value depending on how we execute the containing script. Only the file that you are running has a __name__ set to ‘__main__’.
So what’s the purpose of __name__?
This feature allows you to both create a script which runs itself but also lends other scripts it’s functionality, so to minimise duplicating any coding.
So when you write a script containing a library of functions which can be useful in other places, this feature comes really handy.
Moreover, it allows you to create code that’s more efficient and dynamic for multiple users. You definitely want code that can easily scale as your team grows.
Python is such a popular language because features like this are well thought out and make life for the coder significantly easier. As more languages streamline their functionality to allow the coder to do less work, hopefully, coders have to carry less of the code and the language/compiler can do more.
Given that, features like I just explained can make life a hell of a lot easier, so I encourage you to use them!
Thanks again for reading! If you have any questions, please message =]
Keep up to date with my latest work here!