python中 __name__及__main()__的使用
python中 __name__及__main()__的使用
#hello.py
def sayHello():
str="hello"
print(str); if __name__ == "__main__":
print ('This is main of module "hello.py"')
sayHello()
python作为一种脚本语言,我们用python写的各个module都可以包含以上那么一个累死c中的main函数, 只不过python中的这种__main__与c中有一些区别,主要体现在:
1、当单独执行该module时,比如单独执行以上hello.py:
python hello.py,则输出
This is main of module "hello.py"
hello
可以理解为"if__name__=="__main__":"这一句与c中的main()函数所表述的是一致的,即作为入口;
2、当该module被其它module引入使用时,其中的"if__name__=="__main__":"所表示的Block不会被执行, 这是因为此时module被其它module引用时,其__name__的值将发生变化,__name__的值将会是module的名字。 比如在python shell中import hello后,查看hello.__name__:
>>>import hello
>>>hello.__name__ 'hello'
>>>
3、因此,在python中,当一个module作为整体被执行时,moduel.__name__的值将是"__main__"; 而当一个module被其它module引用时,module.__name__将是module自己的名字,当然一个module被其它module引用时, 其本身并不需要一个可执行的入口main了。可以说python中的这种用法很灵活啊。
python中 __name__及__main()__的使用的更多相关文章
- python中 __name__及__main()__的妙处
python中 __name__及__main()__的妙处 #hello.pydef sayHello(): str="hello" print(str); if __name_ ...
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- 【转】浅析python 中__name__ = '__main__' 的作用
原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...
- 002_浅析python 中__name__ = '__main__' 的作用
很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...
- 理解 python 中__name__ = '__main__' 的作用
很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...
- python 中__name__ = '__main__' 的作用,到底干嘛的?
python 中__name__ = 'main' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and execu ...
- python __name__及__main()__的妙处
#hello.py def sayHello(): str="hello" print(str); if __name__ == "__main__": pri ...
- python学习笔记26(python中__name__的使用)
在python中,每个py文件都是一个模块,也都是一个可执行文件,即包含main方法.因此,对每个py文件,可以单独运行,也可以import它给其他客户使用,这两种情况不一样. 1. 如果模块是被导入 ...
- Python中__name__属性的妙用
在Python中,每一个module文件都有一个built-in属性:__name__,这个__name__有如下特点: 1 如果这个module文件是被别的文件导入的,那么,该__name__属性的 ...
随机推荐
- 使用ASP.NET实现Windows Service定时执行任务
转载http://blog.csdn.net/yanghua_kobe/article/details/6937816 我们怎样才能在服务器上使用asp.net定时执行任务而不需要安装windows ...
- c语言学习之基础知识点介绍(十四):指针的进阶
一.指针的加.减法运算 /* 1.加法运算 1).可以跟整数进行加法运算,得到的还是一个地址 公式: 地址 + 1 = 地址 + 1 * 类型所占的字节数 地址 + n = 地址 + n * 类型所占 ...
- iOS-开发日志-UIPageControl
UIPageControl 1. numberOfPages // 设置有多少页 默认为0 // 2) 设置页数 [pageControl setNumberOfPages:kImageCount] ...
- struts1、 struts2所有版本jar包下载地址大全
jakarta-struts-1.2.2.tar.gz 30-Aug-2004 18:21 12M jakarta-struts-1.2.2.tar.gz.asc 30-Aug-2 ...
- 01_JavaMail_05_创建邮件工具类MailUtils等方便发送邮件
[工程截图] [代码实现] [Mail.java] package com.Higgin.Utils; import java.util.ArrayList; import java.util.Lis ...
- ASP.NET获取上传图片的大小
1.采用客户端javascript可以取得图片大小 <input id="FileUpload" type="file" size="30&qu ...
- js监听
IE浏览器监听: function attachEvent(string eventFlag, function eventFunc) eventFlag: 事件名称,但要加上on,如onclick. ...
- html标签data大写获取不到值:只能小写+横杠命名
html标签data大写获取不到值:只能小写+横杠命名 例如: <i class="glyphicon glyphicon-question-sign" data-tip-t ...
- js json和对象互相转换
http://www.jb51.net/article/44562.htm obj = JSON.parse(string) | obj = jQuery.parseJSON(str) 将JSON字符 ...
- 基于ActiveMQ的点对点收发消息
ActiveMQ是apache的一个开源消息引擎.可以作为即通引擎或者消息中间件引擎. 准备 下载ActiveMQ http://activemq.apache.org/download.html 进 ...