python 教程 第六章、 模块
第六章、 模块
1) 模块
sys模块
字节编译的.pyc文件,优化编译后生成pyo文件
2) from..import语句
import sys
print 'The command line arguments are:'
for i in sys.argv:
print i
print '\n\nThe PYTHONPATH is', sys.path, '\n'
3) __name__
只想在程序本身被使用的时候运行主块,而在它被别的模块输入的时候不运行主块
from sys import *
print 'The command line arguments are:'
for i in argv:
print i
print '\n\nThe PYTHONPATH is', path, '\n'
4) 制造自己的模块
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
print 'Hi, this is mymodule speaking.'
version = '0.1'
# End of mymodule.py
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
5) reload模块重载
>>> import changer #导入模块changer.py文件
>>> changer.printer() # No effect: uses loaded module
First version
##修改changer.py的代码##
>>> from imp import reload
>>> reload(changer) # Forces new code to load/run
<module 'changer' from 'changer.py'>
>>> changer.printer() # Runs the new version now
reloaded: After editing
6) dir()函数
列出模块定义的标识符。标识符有函数、类和变量。
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright'…]
>>>
如果不提供参数,它返回当前模块中定义的名称列表。
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> a = 1
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'sys']
>>> del a
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>
列出数据类型的可使用的函数,help函数的简体版
>>> dir(dict)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
7) 包导入
import dir1.dir2.mod #from dir1.dir2.mod import x
dir1在PYTHONPATH路径中
dir1目录/dir2目录/mod.py文件
dir1目录和dir2目录下都必须含有__init__.py
8) 修改模块搜索路径
sys.path.append('c:\\lp4e\\examples')
python 教程 第六章、 模块的更多相关文章
- Objective-C 基础教程第六章,源文件组织
目录 Object-C 基础教程第六章,源文件组织 0x00:前言 0x01:Xcode创建OC类 0x02:Xcode群组 0x03 Xcode跨文件依赖关系 @class关键字 导入和继承 小结 ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...
- Cobalt Strike系列教程第六章:安装扩展
Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...
- [ABP教程]第六章 作者:领域层
Web开发教程6 作者:领域层 关于此教程 在这个教程系列中,你将要构建一个基于ABP框架的应用程序 Acme.BookStore.这个应用程序被用于甘丽图书页面机器作者.它将用以下开发技术: Ent ...
- Python学习(六)模块
Python 模块 模块定义 随着程序越来越庞大,需要分隔成几个文件:也需要能在不同文件中复用函数.类和变量,而不是拷贝代码.为了满足这些需要,Python提供了模块. 简单来说,模块就是一个保存了P ...
- python学习笔记(六):常用模块
一.模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 什么是包? 包, ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(四)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(四) 1.Subject的代码结构 ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(三)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(三) 1.准备3个Realm MyR ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(二)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(二) 1.Authenticatio ...
随机推荐
- 闪回drop恢复表后sql运行计划异常
-----正常运行计划 set autotrace traceonly set linesize 1000 select /*+index(t idx_object_id)*/ * from t wh ...
- 亿格瑞A5-hdmi故障了
http://cn.egreatworld.com/store/product/egreat-a5-4k-uhd-hdr-blu-ray-navigation-hdd-media-player/ a5 ...
- https://github.com/mvf/svn_wfx
https://github.com/mvf/svn_wfx 2003.net对应的vc是7.0版本.需要更高的. 在哪里可以下载呢 https://www.tjupt.org/没有校外种子 Proj ...
- 建立空间参考 ISpatialReference
转自原文建立空间参考 ISpatialReference ISpatialReferenceFactory spatialReferenceFactory = new SpatialReference ...
- [Angular Directive] Combine HostBinding with Services in Angular 2 Directives
You can change behaviors of element and @Component properties based on services using @HostBinding i ...
- Android Notification如何显示表情?
遇到这种分析用什么实现的,肯定要祭出大杀器Android Device Monitor(AS在Tools->Android)打开之后,选中连接的设备,然后点击小手机图标,即可导出UI层次图.咱们 ...
- HTML代码简写法:Emmet和Haml(转)
HTML代码写起来很费事,因为它的标签多. 一种解决方法是采用模板, 在别人写好的骨架内,填入自己的内容.还有一种就是我今天想要介绍的方法----简写法. 常用的简写法,目前主要是Emmet和Haml ...
- 【60.97%】【BZOJ 1925】 [Sdoi2010]地精部落
Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1194 Solved: 728 [Submit][Status][Discuss] Descript ...
- J2EE学习篇之--JQuery技术具体解释
前面我们解说了的J2EE的技术都是服务端的技术,以下我们来看一下前端的一些开发技术,这一篇我们来看一下jQuery技术 简单介绍: jQuery由美国人John Resig创建,至今已吸引了来自世界各 ...
- Effective C++ 条款14
在资源管理器中小心copying行为 上节是对资源的管理说明.有时候我们不能依赖于shared_ptr或者auto_ptr,所以我们须要自己建立一个资源管理类来管理自己的资源. 比如建立一个类来管理M ...