What does the following do in Python?
-
if __name__ == '__main__': pass When is the if statement true and when is it false? Please explain it simply.
-
Answer:
The if statement is true when the python code is run as a script and false when the code is imported as a module. Suppose I write a bit of code that does something useful - say some numerical computation. As a part of this code, I have written several functions. Let's suppose the code looks something like this: #computation.py def subfunc(data): return modified(data) def otherfunc(data): return changed(data) data = load_data(filename) final = otherfunc(subfunc(data)) write_data(outfilename, final) I've tested this code, and it works great! I can run it from the commandline as python computation.py, it reads data in from filename, performs my computation, and writes out the results to outfilename. Later, I decide that those functions I wrote were very useful, and I'd like to use them again in another piece of code. I know about the dangers of copying and pasting code, so I'll just use import to load them directly from this file. Unfortunately, when I try and do something like from computation import subfunc not only will I load in the function I want to use, it will also try and execute all the other code in the module. So it will try and load in data from filename, modify it, and write the data back out. But in this case, I don't care about all that --- I just want the function. So the thing to do is to go back to computation.py and modify it with an if statment: #computation.py def subfunc(data): return modified(data) def otherfunc(data): return changed(data) if __name__ == '__main__': data = load_data(filename) final = otherfunc(subfunc(data)) write_data(outfilename, final) Now the code in that if statement will run just fine if it's the main script - i.e., if I run it as a stand-alone script. But if it gets run as a part of an import, then it won't be the main script and that loading/writing part won't be done.
Brett Olsen at Quora Visit the source
Other answers
A python module is a file containing Python definitions and expressions. The module name is the name of the file excluding the .py extension. Inside the module __name__ is a global variable which has the name of the module as a string. E.g. if you have a python file sum.py: # sum.py import sys def sum(n): '''Returns sum of first n numbers''' return n * (n + 1) / 2 if __name__ == '__main__': print '__name__ is __main__' print sum(int(sys.argv[1])) You can use the module in other python files. If you run the following on your python prompt, you'll get the name of the module. >>> import sum >>> sum.__name__ 'sum' >>> sum.sum(5) 15 In the import case, since __name__ is 'sum' the condition of the if statement fails and the code below it does not run. Python modules can also be run as independent scripts like: $ python sum.py 5 __name__ is __main__ 15 Here, python sets the variable __name__ to '__main__' , which is utilized to make the module to be used as an independent script as well. In this case the if statement would pass.
Akshit Khurana
Related Q & A:
- What are the common usage of python programming language?Best solution by quora.com
- What’s the difference in meaning between the following pairs of words?Best solution by Yahoo! Answers
- What is the Weather like in the following part of Mexico in Mid January?Best solution by Yahoo! Answers
- What can I do? "The following message could not be delivered to all recipients?Best solution by Yahoo! Answers
- What would the following characters from Aqua Teen Hunger Force do if?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.