一.包,模块,函数:
Python程序由包(package),模块(module)和函数(function)组成。
1.模块是处理某一类问题的集合,由函数和类构成,如os模块用于处理文件的操作,re用于正则表达式。
2.包由一系列模块构成的集合,是一个完成特定任务的工具箱。
Lib目录中的xml文件夹,就是一个包。
此包用于XML的应用开发。文件__init__.py是xml包的注册文件。若没有该文件
Python将不能识别xml包。
1.包必须包含注册文件:__init__.py
2.__init__.py可以为空,它用于标识
文件夹是一个包
二.认识模块(module)
1.模块的创建
一个文件即是一个模块。如module1.py就是模块modulle1。
现在进行自定义模块的创建 (自己编写模块):
(1)创建newModule.py文件
(2)在文件中定义一个函数func()和类myClass 类中定义一个方法myFunc()
(3)导入该模块,并调用其包含的类和函数
#创建newModule.py
#自定义模块
def func():
print("这是func()")
class myClass:
def myFunc(self):
print("这是类中方法:myFunc()")
#同目录下 创建call_newModule.py 调用之
import newModule #导入模块
newModule.func() #调用模块中包含的函数
myclass=newModule.myClass() #生成对象
myclass.myFunc()
练手:
#多次导入同一模块
(1)创建一个模块
(2)包含一个全局变量,一个函数,调用该函数时,全局变量自动+1
(3)多次导入该模块,并调用函数,查看全局变量值
# 模块:newModule.py
count=0 # 全局变量
def func():
global count
count+=1
return count
# 导入模块
import newModule
print("第一次调用:",newModule.func())
newModule.count=10
print("count设置为10:",newModule.count)
import newModule
print("第二次调用:",newModule.func())
#run
第一次调用: 1
count设置为10: 10
第二次调用: 11
------------
多次导入,调用并不会重新生成新的,Python import可以置于程序任意位置
三.模块的属性
内置属性 | 特定任务 |
---|---|
_ _name_ _ | 等于“_ _main_ _”时 该模块作为主程序入口 |
_ _doc_ _ | 为模块编写文档字符串 |
if __name__=='__main__':
print('模块作为主程序运行')
else:
print('模块被调用')
__doc__='''
doc:
module:newModele
test: __name__ __doc__
'''
# call_newModule.py
import newModule
print(newModule.__doc__)
#run
#run newModule:
模块作为主程序运行
#run call_newModule:
模块被调用
doc:
module:newModele
test: __name__ __doc__
四.内联模块(buildin)和内置函数
定义了一些开发中常用的函数,可以直接调用
函数 | 描述 |
abc(x) | 返回x的绝对值 |
bool([x]) | 把一个值或者表达式转换为bool类,如果表达式x为值,返回true;否则返回false |
float(x) | 把数字或者字符串转换为float格式 |
help([object]) | 返回内置函数的帮助说明 |
int(x) | 把数字或字符串转换为整型 |
len(obj) | 对象包含的元素个数 |
range([start,] end [,step]) | 生成一个列表并返回 |
round(x,n=0) | 四舍五入函数 |
type(obj) | 返回一个对象的类型 |
#test
list=range(1,10)
print('list=range(1,10) ',list)
len=len(list)
print('len=len(list) ',len)
falg=bool(1+1)
print('falg=bool(1+1) ',falg)
myfloat=float('1.1')
print('myfloat=float("1.1") ',myfloat)
myint=int('2')
print('myint=int("2") ',myint)
mytype=type('type')
print('mytype=type("type") ',mytype)
doc=help(list)
print('doc=help(list) ',doc)
#run
list=range(1,10) range(1, 10)
len=len(list) 9
falg=bool(1+1) True
myfloat=float("1.1") 1.1
myint=int("2") 2
mytype=type("type") <class 'str'>
Help on range object:
class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
|
| Return an object that produces a sequence of integers from start (inclusive)
| to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
| start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
| These are exactly the valid indices for a list of 4 elements.
| When step is given, it specifies the increment (or decrement).
|
| Methods defined here:
|
| __bool__(self, /)
| self != 0
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __reduce__(...)
| helper for pickle
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(...)
| Return a reverse iterator.
|
| count(...)
| rangeobject.count(value) -> integer -- return number of occurrences of value
|
| index(...)
| rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.
| Raise ValueError if the value is not present.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| start
|
| step
|
| stop
doc=help(list) None # help()返回None 当使用到help()时 会直接输出帮助文档
Qiu TengLong
2018/7/12 14:49