导入模块的目的就是为了调用对应的方法,所以需要查看模块对应的属性和方法,同样在查看其他人的代码时,有时需要查看方法来自哪个模块。
导入模块之后查看模块中的方法:
使用help()方法
In [1]: import mymod
In [2]: help(mymod.sum)
运行结果:
Help on function sum in module mymod:
sum(x, y)
the sum of two numbers
>>> sum(1,2)
3
>>> sum(2,3)
5
使用函数dir()
列出模块的属性和方法
In [4]: dir(mymod)
Out[4]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'sum']
利用函数的属性__doc__
In [8]: print sum.__doc__
sum(sequence[, start]) -> value
Returns the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0). When the sequence is
empty, returns start.
利用函数查看变量
直接使用help()函数
In [10]: help(sum)
Help on built-in function sum in module __builtin__:
sum(...)
sum(sequence[, start]) -> value
Returns the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0). When the sequence is
empty, returns start.
In [1]: from mymod import sum
In [2]: help(sum)
一般情况下,使用from…import…导入的模块。如果代码函数较多时,查看不易,使用help()函数
Help on function sum in module mymod: #可以看出该函数的模块是mymod
sum(x, y)
the sum of two numbers
>>> sum(1,2)
3
>>> sum(2,3)
5