转载: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. python3 csv数据读入/写出

    这是读入 1 import csv 2 #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() 3 with open("XXX ...

  2. Android应用框架-Volley网络通信框架

    1.Volley简介: Volley是Google 推出的 Android 异步网络请求框架和图片加载框架. 在 Google I/O 2013 大会上发布. 2.Volley特点 扩展性强. And ...

  3. c#和c++互操作(平台调用相关)

    [DllImport("ScreenCaptureLib.dll", CallingConvention = CallingConvention.Cdecl)] public st ...

  4. 使用PHP判断是否为微信、支付宝等移动设备访问代码

    在开发过程中经常遇到根据不同的设备显示不同的数据或者在页面样式上做不同的布局,另外在做支付接口的时候也可能会判断当前是什么设备访问,例如判断如果是微信内置浏览器访问则只启用微信支付功能,如果是支付宝内 ...

  5. 前阿里DT总监欧吉良猝死:一代大神勾践陨落滴滴

    欧吉良 阿里巴巴集团数据技术及产品部(DT)总监,淘宝网&天猫BI团队负责人,集团数据委员会数据运营组组长,阿里数据大学校长:2007年7月正式加入阿里,先后在支付宝.天猫.淘宝.数据技术及产 ...

  6. Java知识点汇总

    Java中泛型的本质 Java中静态变量的适用场景 Java类加载原理及类加载器 Java中对Clone的理解 Java中HashMap的实现 Java中Collection和Collections的 ...

  7. 数据的持久性存储(二)——CoreData(附Demo)

    CoreData是一款稳定.功能全面的持久性工具.(本文参考iphone开发3所写,比较简要,需详细了解可以参考iphone开发3) 首先创建一个新的项目CoraData,记得勾选Use Core D ...

  8. 杂项-公司-百科:华特·迪士尼-un

    ylbtech-杂项-公司-百科:华特·迪士尼 华特·迪士尼(Walt Disney,全名Walter Elias Disney,又译沃尔特·迪士尼,1901年12月5日—1966年12月15日),出 ...

  9. IDA Pro 权威指南学习笔记(四) - IDA 用户界面的基本规则

    基本规则: IDA 不提供撤销功能 如果由于不小心按下某个键,导致数据库文件发生意外,这时需要将显示窗口恢复到以前的状态 几乎所有的操作都有其对应的菜单项.热键和工具栏按钮 IDA 的工具栏高度可配置 ...

  10. spring-boot多环境配置文件

    spring-boot多环境配置文件 目录 配置 多环境配置文件名称要遵循格式 application-{profile}.yml application.yml spring: profiles: ...