转载:https://stackoverflow.com/questions/419163/what-does-if-name-main-do

When your script is run by passing it as a command to the Python interpreter,

python myscript.py

all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets run. Unlike other languages, there's no main()function that gets run automatically - the main() function is implicitly all the code at the top level.

In this case, the top-level code is an if block. __name__ is a built-in variable which evaluates to the name of the current module. However, if a module is being run directly (as in myscript.pyabove), then __name__ instead is set to the string "__main__". Thus, you can test whether your script is being run directly or being imported by something else by testing

if __name__ == "__main__":
...

If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed, but the code in the then-body of the if clause above won't get run as the condition is not met. As a basic example, consider the following two scripts:

# file one.py
def func():
print("func() in one.py") print("top-level in one.py") if __name__ == "__main__":
print("one.py is being run directly")
else:
print("one.py is being imported into another module")
# file two.py
import one print("top-level in two.py")
one.func() if __name__ == "__main__":
print("two.py is being run directly")
else:
print("two.py is being imported into another module")

Now, if you invoke the interpreter as

python one.py

The output will be

top-level in one.py
one.py is being run directly

If you run two.py instead:

python two.py

You get

top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly

Thus, when module one gets loaded, its __name__ equals "one" instead of __main__.

python if __name__ == '__main__' 作用的更多相关文章

  1. 浅析python 中__name__ = '__main__' 的作用

    引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...

  2. 【转】浅析python 中__name__ = '__main__' 的作用

    原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...

  3. 002_浅析python 中__name__ = '__main__' 的作用

    很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...

  4. 理解 python 中__name__ = '__main__' 的作用

    很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...

  5. 对于python的__name__="__main__"的含义的理解

    学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = '__main ...

  6. python的__name__ == \'__main__\' 意义

    转自http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...

  7. __name__ __main__ 作用

    1 __name__ 在自己文件下面执行 就显示__main__ 2 如果__name__是在其他文件里面,然后通过当前文件调用到其他文件执行,就会显示的当前文件路劲的文件名结果: if __name ...

  8. 谈谈python 中__name__ = '__main__' 的作用

    最近刚刚学习python,看到别人的源代码中经常出现这样一个代码段: if __name__ = '__main__' dosomting() 觉得很晕,不知道这段代码的作用是什么,后来上网查了一些资 ...

  9. python中__name__=='__main__'的作用

      学习python语法的过程中碰到了__name__=='__main__',这里做个笔记. 作用   这段代码的作用就是让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 测试 先 ...

随机推荐

  1. SSM框架——Spring+SpringMVC+Mybatis的搭建

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  2. 剑指offer-第五章优化时间和空间效率(在字符串中第一次出现切只出现一次的字符)

    题目:在字符串中第一次出现切只出现一次的字符 思路:用HashMap来存放对应的char值和该char出现的次数.做一次变量就可以得到第一个只出现一次的字符. Java代码: import java. ...

  3. 剑指offer-第四章解决面试题思路(判断一个数组是否为二叉搜索树的后序遍历序列)

    二叉搜索树:二叉搜索树根节点的左边都比根节点小,右边都比根节点大. 例题:输入一个数组,判断是否为二叉搜索树的后序遍历序列,如果是,返回true,如果不是,返回flase,假设没有重复的元素. 思路: ...

  4. 【WCF安全】使用X509证书自定义验证

    接触WCF时间比较短,在项目中要使用X509证书,纠结好几天终于有了结论,因此为了方便日后查阅和园友交流特意单独将部分代码提出,并做以记录. 1.准备工作 制作X509证书,此处用到三个证书名称 导入 ...

  5. jstl_fn方法库

    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 1,st ...

  6. eclipse怎么在项目里面批量替换单词

    先选中你要替换的东东,然后再菜单栏中找到Search→Text→Project(先创建要搜索的project),这样就会在整个项目中查找单词.然后在Search的Console中,单击项目,右键选择R ...

  7. Eclipse里git提交冲突rejected – non-fast-forward

    Eclipse里commit代码,其实只是提交到本地仓库,需要push才会提交到远程的git仓库,这时是一个本地仓库到远程仓库的同步过程.Git是分布式的,每个人在本地仓库维护本地的自己的那一份代码, ...

  8. Crypto 加密解密

    import binascii from Crypto.Cipher import AES #秘钥,此处需要将字符串转为字节 from utils import config from utils.e ...

  9. Jmeter录制HTTPS 补充

    Jmeter有录制功能,录制HTTPs需要增加一个证书配置,录制步骤如下: 1.打开jmeter,添加线程组.线程组右键,逻辑控制器>录制控制器 工作台 右键 非测试元件 >HTTP代理服 ...

  10. 2016女生赛 HDU 5710 Digit-Sum(数学,思维题)

    Digit-Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...