#掌握探索模块的重要性在于你很多时候不需要把每个模块的功能和方法掌握的那么清晰
import math
#导入一个模块,你知道这是数学模块math,但你知道这里面都有什么函数吗,以及对math的解释是什么
1.dir
#查明模块包含什么,列出对象的所有属性,对于模块,它列出所有的函数、类、变量等
a=[n for n in dir(math)]
print(a)
# ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
#列出了超多内容的一大串列表,这些就是math这个模块中所有的函数、类、变量
#你应该已经知道开头下划线的函数或属性都是私密的,尽管你可以用一种不怎么合适的方法直接访问它们,但正常情况下你只希望看到那些公开的函数和属性
b=[n for n in dir(math) if n.find("_")!=0]
print(b)
# ['acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
2.__all__变量
#对于一些正规的模块,__all__是很重要的,它告诉开发者模块的公有接口是什么,并且告诉解释器这个模块导入全体后哪些东西直接就可以用
#像from math import *
import copy
print(copy.__all__)#['Error', 'copy', 'deepcopy']
#这里用copy做例子,因为math显然没有__all__这个属性
#当你自己写一些模块时,定义变量__all__可以让其他使用你模块的人更快的理解你的设计
3.doc
#不知你是否还记得第六章第一节中的say_hello函数,函数第一行是一段字符串,那就是__doc__文档字符串,用于解释函数
print(math.__doc__)
# This module is always available. It provides access to the
# mathematical functions defined by the C standard.
print(math.sin.__doc__)
# sin(x)
# Return the sine of x (measured in radians).
#这大致解释了模块和模块中函数的意义,但还有一个方法可以获得更详细的内容
3.help方法
print(help(math.sin))
# Help on built-in function sin in module math:
# sin(...)
# sin(x)
# Return the sine of x (measured in radians).
# None
#这可能和__doc__差不了多少,但是它在许多时候都更能解释它是干什么用的
4.内置方法的__doc__
#对许多内置方法,__doc__可以直接告诉你需要哪些参数,下面那一大堆都是解释
print(range.__doc__)
# 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).