python模块

1. 什么是模块

模块是函数功能的扩展,可以实现一项或多项功能模块。

一般模块都是在 pythonXX/lib 下面

2. 如果导入一个模块

import

例如使用pi

我们可以如下操作:

import math
math.pi
执行结果:

>>> math.pi
3.141592653589793


3. sys模块,也可以称为标准模块

sys的实例,

查看python版本:

import sys 
sys.vesion

执行结果

'2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)]'


运行程序的目录:

sys.executable

执行结果:

'D:\\Python27\\pythonw.exe'


获取windows版本

>>> sys.getwindowsversion()

执行结果:
sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')


还有获取支持的模块:

>>> sys.modules.keys()

结果太长就不截图和贴出来了。


************************************************************************************************************

字节编译

字节编译就是把.py文件编译成二进制的.pyc文件,后续执行可以直接执行.pyc二进制文件。 


字节编译跟c++编译差别

字节编译说是说编译,其实不是用编译器生成的,而是通过解释器生成的。故跟c++的编译有本质区别。


.pyc文件的生成 方法有两种,

一种是通过gui工具,输入需要执行的模块,按enter键执行,就会生成 。

例如 import zipfile 

+ enter 键输入 

就会在lib 目录下生成zipfile.pyc 

另一种是,直接在cmd中执行 python -m compileall XXX/XXX/lib/xmllib.py

*************************************************************************************************************

from ... import 

导入 ... 中具体一个方法


from ... import *

导入...模块的所有方法


实例:

>>> from sys import version

>>> version

 '2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)]'

对比上面学习,如果只是导入了sys,则此处需要输入 sys.version 否则报错 。


>>> from sys import *

>>> getwindowsversion()

sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')

>>> executable

'D:\\Python27\\pythonw.exe'


***************************************************************************************************************

认识__name__属性 


__name__的值是__main__,那就是主模块。


实例:

在gui中输入如下代码,并保存为 lname.py

#coding utf-8
# -*- coding: cp936 -*-
#首先我们分别看一下这个模块在不同的场景中的__name__的值

print __name__

#其次我们看一下__name__属性的常用情况

if __name__ == "__main__" :
    print "It is a main"
else :
    print "It is not main"

然后我们分两种方式运行,

一种直接用F5来执行,这时候就是主模块,

运行结果:

__main__
It is a main

如果我们通过调用 

import lname 

这时候运行结果就是 :

lname
It is not main

***************************************************************************************************************

自定义模块

就是自己定义编写的模块

保存在Lib下面


实例:

自定义一个addFromWlh.py在Lib目录下

i=0
j=0
def add(i,j):
    k=i + j
    return k
k =add(i,j)
print k


然后在执行时候,

>>> import addFromWlh
0
>>> i = 3
>>> j = 4
>>> addFromWlh.add(i,j)
7

即使实现一个加法的模块功能。


*****************************************************************************************************

dir()函数

可以用来查看指定模块的功能列表


实例:

>>>import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_git', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_traceback', 'exc_type', 'exc_value', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
>>> sys.platform
'win32'
>>> sys.api_version
1013


dir()函数的扩展

可以查看任意对象的功能列表

>>> d=[]
>>> dir(d)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

>>> e=()
>>> dir(e)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

>>> u='a'
>>> dir(u)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

这样我们简单的学习了写模块概念,字节编码,导入模块,认识模块的属性,自定义模块,dir()函数的用法的模块相关的属性。










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值