python学习-模块与包(九)

9.4查看模块内容

dir(): 返回模块或类所包含的全部程序单元(包括变量、函数、类和方法等)

__all__:模块本身提供的变量,不会展示以下划线开头的程序单元。另使用from xx import *,是不会导入以下划线开头的程序单元的

import logging,pprint
pprint.pprint(dir(logging))
['BASIC_FORMAT',
 'BufferingFormatter',
 'CRITICAL',
 'DEBUG',
 'ERROR',
 'FATAL',
 'FileHandler',
 'Filter',
 'Filterer',
 'Formatter',
 'Handler',
 'INFO',
 'LogRecord',
 'Logger',
 'LoggerAdapter',
 'Manager',
 'NOTSET',
 'NullHandler',
 'PercentStyle',
 'PlaceHolder',
 'RootLogger',
 'StrFormatStyle',
 'StreamHandler',
 'StringTemplateStyle',
 'Template',
 'WARN',
 'WARNING',
 '_STYLES',
 '_StderrHandler',
 '__all__',
 '__author__',
 '__builtins__',
 '__cached__',
 '__date__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__status__',
 '__version__',
 '_acquireLock',
 '_addHandlerRef',
 '_checkLevel',
 '_defaultFormatter',
 '_defaultLastResort',
 '_handlerList',
 '_handlers',
 '_levelToName',
 '_lock',
 '_logRecordFactory',
 '_loggerClass',
 '_nameToLevel',
 '_releaseLock',
 '_removeHandlerRef',
 '_showwarning',
 '_srcfile',
 '_startTime',
 '_warnings_showwarning',
 'addLevelName',
 'atexit',
 'basicConfig',
 'captureWarnings',
 'collections',
 'critical',
 'currentframe',
 'debug',
 'disable',
 'error',
 'exception',
 'fatal',
 'getLevelName',
 'getLogRecordFactory',
 'getLogger',
 'getLoggerClass',
 'info',
 'io',
 'lastResort',
 'log',
 'logMultiprocessing',
 'logProcesses',
 'logThreads',
 'makeLogRecord',
 'os',
 'raiseExceptions',
 'root',
 'setLogRecordFactory',
 'setLoggerClass',
 'shutdown',
 'sys',
 'threading',
 'time',
 'traceback',
 'warn',
 'warning',
 'warnings',
 'weakref']
pprint.pprint(logging.__all__)
['BASIC_FORMAT',
 'BufferingFormatter',
 'CRITICAL',
 'DEBUG',
 'ERROR',
 'FATAL',
 'FileHandler',
 'Filter',
 'Formatter',
 'Handler',
 'INFO',
 'LogRecord',
 'Logger',
 'LoggerAdapter',
 'NOTSET',
 'NullHandler',
 'StreamHandler',
 'WARN',
 'WARNING',
 'addLevelName',
 'basicConfig',
 'captureWarnings',
 'critical',
 'debug',
 'disable',
 'error',
 'exception',
 'fatal',
 'getLevelName',
 'getLogger',
 'getLoggerClass',
 'info',
 'log',
 'makeLogRecord',
 'setLoggerClass',
 'shutdown',
 'warn',
 'warning',
 'getLogRecordFactory',
 'setLogRecordFactory',
 'lastResort',
 'raiseExceptions']
dir()、__all__

使用__doc__属性查看文档。另:使用help()函数查看的其实就是程序单元的__doc__属性值

import string
help(string.capwords)
Help on function capwords in module string:
capwords(s, sep=None)
    capwords(s [,sep]) -> string
    
    Split the argument into words using split, capitalize each
    word using capitalize, and join the capitalized words using
    join.  If the optional second argument sep is absent or None,
    runs of whitespace characters are replaced by a single space
    and leading and trailing whitespace are removed, otherwise
    sep is used to split and join the words.
print(string.capwords.__doc__)
capwords(s [,sep]) -> string
    Split the argument into words using split, capitalize each
    word using capitalize, and join the capitalized words using
    join.  If the optional second argument sep is absent or None,
    runs of whitespace characters are replaced by a single space
    and leading and trailing whitespace are removed, otherwise
    sep is used to split and join the words.
__doc__与help()

使用__file__属性查看模块的源文件路径

import string
string.__file__
'E:\\soft\\Python\\Python36\\lib\\string.py'
__file__

猜你喜欢

转载自www.cnblogs.com/wang-mengmeng/p/11520564.html