第六章、 模块

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. 将OpenCV捕获的摄像头加载到picture控件中

    CRect rect; CStatic* pStc; CDC* pDC; HDC hDC; pStc = (CStatic*)GetDlgItem(IDC_CAM);//IDC_CAM是Picture ...

  2. Effective C++ 条款28

    避免返回handles指向对象内部成分 本节作者讲述的知识核心是对于一个类来说,应该避免类返回自己内部的私有数据. 例如以下: class Point{ public: Point(int x, in ...

  3. mysql 查询字段名所在的表

    select * from (select * from information_schema.COLUMNS where table_schema = '数据库名') temp where colu ...

  4. 【topcoder SRM 702 DIV 2 250】TestTaking

    Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...

  5. hadoop一些常见报错的解决方式

    Failed to set setXIncludeAware(true) for parser 遇到此问题通常是jar包冲突的问题. 一种情况是我们向java的lib文件夹加入我们自己的jar包导致h ...

  6. [Angular Unit Testing] Shallow Pipe Testing

    import { TestBed, ComponentFixture } from '@angular/core/testing'; import { BrowserDynamicTestingMod ...

  7. [SCSS] Organize SCSS into Multiple Files with Partials

    Tired of dealing with monolithic CSS files? Are requests for multiple CSS files hurting your perform ...

  8. 网络编程02---HTTP协议

    1.URL简单介绍 1.client怎样找到server 我们都知道网络中部署着各种各样的server.比方腾讯的server.百度的server.那么问题来了.client怎样找到想要连接的serv ...

  9. 后台返回的HTML整个页面代码打开方法

    后台返回的html代码片段,需要插入html标签中,而返回的整个html文档,则需要重写整个页面. 解决方法: 需要一个中转页面,用document.write()方法重写整个页面: // POST任 ...

  10. [Javascript] Identify and Deal with NaN in JavaScript

    Dealing with the special NaN value can be tricky in JavaScript. It behaves like a number and not a n ...