python笔记(模块)
2015-12-11 21:07
369 查看
模块
模块的_name_每个模块都有一个名称,在模块中可以通过语句来找出模块的名称。这在一个场合特别有用——就如前面所提到的,当一个模块被第一次输入的时候,这个模块的主块将被运行。假如我们只想在程序本身被使用的时候运行主块,而在它被别的模块输入的时候不运行主块,我们该怎么做呢?这可以通过模块的name属性完成。
使用模块的_name_
#!/usr/bin/python # Filename: using_name.py if __name__ == '__main__': print 'This program is being run by itself' else: print 'I am being imported from another module'
输出
$ python using_name.py This program is being run by itself $ python >>> import using_name I am being imported from another module >>>
相关文章推荐
- Python--类-例子
- 常见的python的unitest用法
- Python 一行命令ftp服务器
- Python赋值语句与深拷贝、浅拷贝的区别
- Python: 消除print的自动换行
- python练习
- python 基本数据类型
- Python调用DLL
- python练习
- python strip()函数
- Robot自定义python关键字库通过ice中间件调用服务进行API测试
- 零基础入门学习Python(7):元组
- python安装以及配置Numpy和Scipy模块
- python学习---列表
- 第二个UI脚本--Python+selenium之unittest+HTMLtestRunner及python的继承
- 深入源码解析Python中的对象与类型
- python对象类型
- python 写三级菜单
- python 读取文件指定某行
- Python中web.py运行后窗口显示中文