Http://www.cnblogs.com/xuxm2007/archive/2010/08/04/1792463.html
When you open a. py file, you will often see if __name__ = = ' __main__ ' at the bottom of the code: Now let's introduce its role.
The module is an object, and all modules have a built-in property of __name__. The value of the __name__ of a module depends on how you apply the module. If you import a module, the value of the module __name__ is usually the module file name, without the path or file extension. But you can also run the module directly like a standard program, in which case the __name__ value will be a special default "__main__".
///////////////////////////////////////////////////////////////////////////////////////////////////
Run the. py file directly in cmd, then the value of __name__ is ' __main__ ';
After import a. py file, the value of __name__ is not ' __main__ ';
Thus use if __name__ = = ' __main__ ' to determine whether the. py file is running directly
Such as:
#Test. py
Class Test:
def __init (self):p
def f (self):p rint ' Hello, world! '
if __name__ = = ' __main__ ':
Test (). F ()
#End
You enter in cmd:
C:>python test.py
Hello, world!.
Description: "__name__ = = ' __main__ '" is established
You type in cmd again:
C:>python
>>>import Test
>>>test.__name__ #Test模块的__name__
' Test '
>>>__name__ #当前程序的__name__
' __main__ '
No matter what, test.py in the "__name__ = = ' __main__ '" will not be set up!
So, the next line of code will never run to!
//////////////////////////////////////////////////////////////////////////////////
Http://zhidao.baidu.com/link?url=12yWQq3fKp4vZ5i-uFAtPsp-69owQh3vcjmTFBACaoOln5VqhJAZoTpjSg4F313LXvr7_xeHFQxaEhlZOm6RG_
__name__ is a method that indicates how the current py file is called. If it is equal to "__main__" means to execute directly, if not, it is used to be called by another file, this time if is false, then it will not execute the outermost code.
Like you have a Python file inside.
Def XXXX ():
#body
Print "ASDF"
In this case, even if you import this file elsewhere, to invoke this XXXX function, it will also execute print "asdf", because he is the outermost code, or called the Global Code. But often we want to only run some code when I execute the file, not (that is, called), then do not execute the code, so it is generally changed to
Def XXXX ():
#body
If __name__= "__main__":
Print "ASDF"
http://keliang.blog.51cto.com/3359430/649318
Presumably many of the first contact Python students will see such a statement, if __name__ = = "__main__":
So what exactly does this statement do? Before explaining, the first thing to declare is that no matter how small you are, you must know:
The suffix of the 1.python file is. py;
2..py files can be used for direct execution, like a small program, can also be used as a module to be imported (such as 360 security guard, is to rely on a functional module to achieve, such as 360 security guard itself framework is a desktop, and the above icon is a shortcut, These shortcuts point to this feature module)
3. Importing a module in Python typically uses the import
Well, after confirming that you know the above, you can start explaining if __name__ = = "__MAIN__": this statement.
First explain if, as the name implies, if is the meaning of the sentence at the beginning of the addition of if, it means that this sentence is a conditional statement. The use of the IF statement is very simple, and of course it takes a lot of practice to really be flexible.
And then __name__,__name__ as the built-in properties of the module, simply put, that is, how to call the. py file.
Finally, __main__, as I mentioned earlier,. py files are used in two ways: as modules are called and used directly. If it equals "__main__" it means direct execution.
Summary: In the If __name__ = = "__main__": After the statement as the module is called, the code after the statement is not executed; Direct use, the code after the statement executes. Typically, this statement is used in module tests.
-------------------------------------------------
if __name__ == "__main__": ...The trick exists in Python so and our Python files can act as either reusable modules, or as standalone programs. A s A toy example, let's say, we have both files:
mumak:~ dyoo$ cat mymath.py
mymath.py文件
| 12345 |
defsquare(x): return x * x if __name__ == ‘__main__‘: print"test: square(42) ==", square(42) |
mumak:~ dyoo$ cat mygame.py
mygame.py file
| 12345 |
importmymath print "this is mygame." printmymath.square(17) |
In this example, we've written mymath.py to being both used as a utility module, as well as a standalone program. We can run MyMath standalone by doing this:
mumak:~ dyoo$ python mymath.pytest: square(42) == 1764
But we can also use mymath.py as a module; Let's see how happens when we run mygame.py:
mumak:~ dyoo$ python mygame.pythis is mygame.289
Notice that here we don't see the ' Test ' line, Mymath.py had near the bottom of its code. That's because, in this context, MyMath is isn't the main program. That's what the if __name__ == "__main__": ... trick was used for.
In this example, when the square function is called in mygame.py, the main function inside the mymath.py is not executed.
In Python if __name__ = = ' __main__ ': parsing