第六章、 模块

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 教程 第六章、 模块的更多相关文章

  1. Objective-C 基础教程第六章,源文件组织

    目录 Object-C 基础教程第六章,源文件组织 0x00:前言 0x01:Xcode创建OC类 0x02:Xcode群组 0x03 Xcode跨文件依赖关系 @class关键字 导入和继承 小结 ...

  2. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...

  3. Cobalt Strike系列教程第六章:安装扩展

    Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...

  4. [ABP教程]第六章 作者:领域层

    Web开发教程6 作者:领域层 关于此教程 在这个教程系列中,你将要构建一个基于ABP框架的应用程序 Acme.BookStore.这个应用程序被用于甘丽图书页面机器作者.它将用以下开发技术: Ent ...

  5. Python学习(六)模块

    Python 模块 模块定义 随着程序越来越庞大,需要分隔成几个文件:也需要能在不同文件中复用函数.类和变量,而不是拷贝代码.为了满足这些需要,Python提供了模块. 简单来说,模块就是一个保存了P ...

  6. python学习笔记(六):常用模块

    一.模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 什么是包? 包, ...

  7. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(四)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(四) 1.Subject的代码结构 ...

  8. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(三)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(三) 1.准备3个Realm MyR ...

  9. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(二)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(二) 1.Authenticatio ...

随机推荐

  1. [Angular] Alternative Themes - Learn the Host-Context Selector

    To add alernative theme, we can use :host-context() selector from Angular. //au-fa-input-red-theme.c ...

  2. [AngularJS NG-redux] Integrate Redux Devtools

    In this lesson, we are going to learn how to integrate Redux Devtools into our Angular application. ...

  3. [Angular2 Form] Create custom form component using Control Value Accessor

    //switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...

  4. iOS开发runtime学习:一:runtime简介与runtime的消息机制

    一:runtime简介:也是面试必须会回答的部分 二:runtime的消息机制 #import "ViewController.h" #import <objc/messag ...

  5. Lucene学习总结之五:Lucene段合并(merge)过程分析 2014-06-25 14:20 537人阅读 评论(0) 收藏

    一.段合并过程总论 IndexWriter中与段合并有关的成员变量有: HashSet<SegmentInfo> mergingSegments = new HashSet<Segm ...

  6. [Node.js] Create a model to persist data in a Node.js LoopBack API

    In this lesson you will learn what a LoopBack model is, you will create a Product model using the Lo ...

  7. jquery平滑滚动页面

    滚动到顶部 $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); 滚动到指定位置 ...

  8. 博客搬家啦! -----> http://ronghaopger.github.io/

    新地方: http://ronghaopger.github.io/ 以后这里就不更新了,感谢博客园!

  9. Freemarker中的日期转换

    1. 把数字类型表示的日期,转换成datetime类型,字符串输出.${item.time?number_to_datetime},默认的格式是"yyyy-MM-dd hh:mm:ss&qu ...

  10. 数据预处理(normalize、scale)

    matlab 工具函数(三)-- normalize(归一化数据) 注:待处理的数据 X∈Rd×N,N 表示样本的个数,d 则是单个样本的维度: 1. 去均值(remove DC) X = bsxfu ...