Python基础-main
Python基础-_main_
写在前面
如非特别说明,下文均基于
Python3
一、__main__的官方解释
参考 _main_ -- Top-level script environment
'_main_' is the name of the scope in which top-level code executes. A module’s _name_ is set equal to '_main_' when read from standard input, a script, or from an interactive prompt.
A module can discover whether or not it is running in the main scope by checking its own _name_, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:
if __name__ == "__main__":
# execute only if run as a script
main()
For a package, the same effect can be achieved by including a _main_.py module, the contents of which will be executed when the module is run with -m.
__main__是顶层代码执行环境的名字。当一个模块从标准输入,脚本或者解释器提示行中被读取时,模块的__name__属性被设置成__main__。
模块可以依据检查__name__属性是否为__main__的方式自我发现是否在main scope中,这允许了一种在模块中条件执行代码的常见用法,当模块作为脚本或者使用python -m命令运行时执行,而被导入时不执行:
if __name__ == "__main__":
# 当且仅当模块作为脚本运行时执行main函数
main()
对于包而言,可以在包中包含一个名为__main__.py的模块到达相同的效果,当模块使用python -m命令运行时,__main__py模块的内容会被执行。
二、__main__实践
第一节中说过,当模块从标准输入,脚本或者解释器命令行中被读取时,其__name__属性被设置为__main__ ,导入时值为模块名字。那么就可以根据这两种不同情况执行不同的代码。
Make a script both importable and executable
让脚本即可执行又可被导入,是对__main__这一特性最好的诠释:
def test():
print('This is test method in com.richard.other') def main():
test() if __name__ == '__main__': print('直接运行时,__name__属性:', __name__)
main()
else:
# 导入时被执行
print('导入时,__name__属性:', __name__)
直接运行时,输出:
直接运行时,__name__属性: __main__
This is test method in com.richard.other
导入时,输出:
>>> import com.richard.other.test as ctest
导入时,__name__属性: com.richard.other.test
>>>
这样,可以在if __name__ == '__main__':的判断下加入对这个模块的测试代码,就可以在不影响外部模块引用该模块的条件下,调试我们的模块了。
NOTICE: 示例在python 3+解释器中成功运行,的模块名为test.py,其包结构如下:

目录sublime已加入python解释器sys.path变量中。
For a package, the same effect can be achieved by including a _main_.py module, the contents of which will be executed when the module is run with -m.
Python基础-main的更多相关文章
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
- python基础之循环结构以及列表
python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.python IDE的选择 IDE的全称叫做集成 ...
- 改写《python基础教程》中的一个例子
一.前言 初学python,看<python基础教程>,第20章实现了将文本转化成html的功能.由于本人之前有DIY一个markdown转html的算法,所以对这个例子有兴趣.可仔细一看 ...
- python基础——单元测试
python基础——单元测试 如果你听说过“测试驱动开发”(TDD:Test-Driven Development),单元测试就不陌生. 单元测试是用来对一个模块.一个函数或者一个类来进行正确性检验的 ...
- python基础——调试
python基础——调试 程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是 ...
- python基础——错误处理
python基础——错误处理 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数 ...
- python基础——filter函数
python基础——filter函数 Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函 ...
- Python自动化 【第七篇】:Python基础-面向对象高级语法、异常处理、Scoket开发基础
本节内容: 1. 面向对象高级语法部分 1.1 静态方法.类方法.属性方法 1.2 类的特殊方法 1.3 反射 2. 异常处理 3. Socket开发基础 1. ...
- Python基础学习笔记(十一)函数、模块与包
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-functions.html 3. http://www.liao ...
随机推荐
- No orientation specified, and the default is horizontal.异常处理(转)
参考:http://blog.csdn.net/sky_monkey/article/details/21466975 整的错误提示信息为: No orientation specified, and ...
- 基础复习之HTML (meta标签、块级元素与行内元素)
一.meta标签 SEO 如何在不使用JS的情况下刷新页面(http-equiv="refresh" , content="time") 设置页面缓存 移动端设 ...
- jenkins实现手动选择分支构建项目-Git Paramater
先下载插件: Git Paramater 参照: jenkins-参数化构建(三)插件:Git Parameter https://www.cnblogs.com/zhaojingyu/p/98624 ...
- 用 Flask 来写个轻博客 (32) — 使用 Flask-RESTful 来构建 RESTful API 之一
目录 目录 前文列表 扩展阅读 RESTful API REST 原则 无状态原则 面向资源 RESTful API 的优势 REST 约束 前文列表 用 Flask 来写个轻博客 (1) - 创建项 ...
- Boosting Ensemble and GBDT Algorithm
Boosting Ensemble: 机器学习中,Ensemble model除了Bagging以外,更常用的是Boosting.与Bagging不同,Boosting中各个模型是串行的.其思想是,后 ...
- HTML创建文本框的3种方式
我的第一个随笔,记录主要用来整理学习的知识点 1.input 创建单行文本框 <input type="text" size="10" maxlength ...
- css设置图片的高等于图片的高
<div class="box"> <img src="img/2222.jpg" /> </div> .box { pos ...
- smb.conf免密登录文件
# This is the main Samba configuration file. You should read the# smb.conf(5) manual page in order t ...
- WPF-将DataGrid控件中的数据导出到Excel
原文:WPF-将DataGrid控件中的数据导出到Excel 导出至Excel是非常常见,我们可以用很多类库,例如Aspose.NOPI.Interop,在这里我们使用微软自家的工具.我的WPF绑定的 ...
- elasticsearch 父子关系
ElasticSearch 中的Parent-Child关系和nested模型是相似的, 两个都可以用于复杂的数据结构中,区别是 nested 类型的文档是把所有的实体聚合到一个文档中而Parent- ...