转载: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,

  1. 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

  1. if __name__ == "__main__":
  2. ...

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:

  1. # file one.py
  2. def func():
  3. print("func() in one.py")
  4.  
  5. print("top-level in one.py")
  6.  
  7. if __name__ == "__main__":
  8. print("one.py is being run directly")
  9. else:
  10. print("one.py is being imported into another module")
  1. # file two.py
  2. import one
  3.  
  4. print("top-level in two.py")
  5. one.func()
  6.  
  7. if __name__ == "__main__":
  8. print("two.py is being run directly")
  9. else:
  10. print("two.py is being imported into another module")

Now, if you invoke the interpreter as

  1. python one.py

The output will be

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

If you run two.py instead:

  1. python two.py

You get

  1. top-level in one.py
  2. one.py is being imported into another module
  3. top-level in two.py
  4. func() in one.py
  5. 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. MySql必知必会实战练习(四)主键、外键、sql约束、联结表

    本博将对主键.外键.MySql数据库约束和联结表的相关特性进行总结和实战 1. 主键 表中的每一行都应该具有可以唯一标识自己的一列(或一组列),而这个承担标识作用的列称为主键 如果没有主键,数据的管理 ...

  2. HTML中id与name的用法

    可以说几乎每个做过Web开发的人都问过,到底元素的ID和Name有什么区别阿?为什么有了ID还要有Name呢? 而同样我们也可以得到最经典的答案:ID就像是一个人的身份证号码,而Name就像是他的名字 ...

  3. WCF OpenTimeout, CloseTimeout, SendTimeout, ReceiveTimeout

    1.OpenTimeout 客户端与服务端建立连接时,如果超过指定时间都还没完成,就引发TimeoutException. 在TCP通讯中,服务器必须首先准备好侦听端口并在该端口上侦听(Listen) ...

  4. 对django整个网站搭建文件的总结

    1.比方说现在是写一个使用HTTP 2.0的HTTP服务器,在不改变现有程序配置的情况下,需要重新编译HTTP服务器,比方说,用的是Nginx服务器,需要隐藏服务器版本,如果想要小绿锁 也就是网站安全 ...

  5. 双击 cui

    //改变属性块的双击事件 //将菜单文件中的双击改一下,退出时还原文件 acad.bak.cui 改这个名字 每次用这个更新为新的 acad.cui进行修改 //退出时再用 acad.bak.cui还 ...

  6. 通过ssh连接git操作

    项目又一次从svn切换到git,而且要求使用安全协议ssh连接git.不管是个人还是组织,git上只负责导入公钥,而不会帮你生成.这里先说下怎么生成这个ssh的私钥和公钥. 首先你得找到有ssh命令的 ...

  7. C#去除数组空格

    static void Main(string[] args) { "}; Console.WriteLine("输出带有空字符串的数组:"); foreach (str ...

  8. sqlbulkcopy 使用DataTable作为数据源的数据类型问题--来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier

    今天做批量插入的时候,SQLSERVER总是报错,错误提示“来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier”. 首先核对了一下定义的dataTable ...

  9. 6kzz整合ueditor

    用过6kzz免费版的朋友都知道,它的后台编辑器是kindeditor,并且是两三年前的版本,有很多功能都没有,现在的kindeditor也不错,但感觉还是没有百度的ueditor强大(据说微信后台的编 ...

  10. 【转】JMeter远程测试

    详解JMeter远程测试(1) 如果运行JMeter客户端的机器性能不能满足测试需要,那么测试人员可以通过单个JMeter GUI客户端来控制多个远程JMeter服务器,以便对服务器进行压力测试,模拟 ...