>>> from math import sqrt
>>> import math 1.2 编写自己的模块
任何python程序都可以作为模块导入。程序保存的位置很重要。假设我们所写的程序叫hello.py被保存在c:/python目录下,可以执行以下的代码,告诉解释器在哪里寻找hello.py模块:
>>> import copy
>>> [n for n in dir(copy) if not n.startswith('_')]
['Error', 'PyStringMap', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']
>>> [n for n in dir(copy) if n.startswith('__')]
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__'] (2)继续查看它的__all__
>>> help(copy)
Help on module copy:
NAME
copy - Generic (shallow and deep) copying operations.
FILE
c:\python27\lib\copy.py
DESCRIPTION
Interface summary:
import copy
x = copy.copy(y) # make a shallow copy of y
x = copy.deepcopy(y) # make a deep copy of y
.......
3.3 文档--模块信息的自然来源
比如寻找range的文档描述:
>>> print range.__doc__
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
这样就获得了关于range函数的精确描述。但是要记住,并不是所有的模块和函数都有不错的文档字符串的,有时候要透彻理解模块和函数是如何工作的,最好参考Pyhton库:http:python.org/doc/lib