本文主要是介绍Python3.2官方文档--标准接口和dir()函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
4.2 标准接口
Python自带一些标准模块的库文件。这些库文件介绍在单独的文档(python库文件介绍)中有所描述。一些模块在解释器中创建,它们提供了对非语言核心的但又为了效率又不得包含到里面部分的操作方法,或者是提供了对操作系统的底层的访问,例如系统调用。这些模块ge会根基底层平台进行不同的选择配置,比如:winreg模块只能在window系统上提供, 另一个特殊模块更值得注意,sys,它内置在每个python解释器中,变量sys.ps1和sys.ps2 定义了主提示符和此提示符使用的字符串。
>>> import sys
>>> sys.ps1
’>>> ’
>>> sys.ps2
’... ’
>>> sys.ps1 = ’C> ’
C> print(’Yuck!’)
Yuck!
C>
只有当解释器处于交互模式时候,这两个变量才能被定义。
变量sys.path 是一系统决定在解释器搜索模块路径的字符串列表。它从默认的环境变量pythonPATH或者当PYTHONPATH为空时候从内置的默认路径进行初始化。你可以用标准列表操作进行修改。
>>> import sys
>>> sys.path.append(’/ufs/guido/lib/python’)
4.3 dir()函数
内置函数dir用来寻找查找模块定义的名称。它返回一个排序后的字符串列表。
>>> import fibo, sys
>>> dir(fibo)
[’__name__’, ’fib’, ’fib2’]
>>> dir(sys)
[’__displayhook__’, ’__doc__’, ’__excepthook__’, ’__name__’, ’__stderr__’,
’__stdin__’, ’__stdout__’, ’_getframe’, ’api_version’, ’argv’,
’builtin_module_names’, ’byteorder’, ’callstats’, ’copyright’,
’displayhook’, ’exc_info’, ’excepthook’,
’exec_prefix’, ’executable’, ’exit’, ’getdefaultencoding’, ’getdlopenflags’,
’getrecursionlimit’, ’getrefcount’, ’hexversion’, ’maxint’, ’maxunicode’,
’meta_path’, ’modules’, ’path’, ’path_hooks’, ’path_importer_cache’,
’platform’, ’prefix’, ’ps1’, ’ps2’, ’setcheckinterval’, ’setdlopenflags’,
’setprofile’, ’setrecursionlimit’, ’settrace’, ’stderr’, ’stdin’, ’stdout’,
’version’, ’version_info’, ’warnoptions’]
没有参数dir()会遍历当前你已经定义的模块名称。
>>> a = [1, 2, 3, 4, 5]
>>> import fibo
>>> fib = fibo.fib
>>> dir()
[’__builtins__’, ’__doc__’, ’__file__’, ’__name__’, ’a’, ’fib’, ’fibo’, ’sys’]
注意它会遍历所有类型的名称:那变量 模块 和函数等等
Dir()无法遍历出内置函数和变量的名称, 如果你想要那样的一个列表,在标准模块buitins中它们被定义。
>>> import builtins
>>> dir(builtins)
[’ArithmeticError’, ’AssertionError’, ’AttributeError’, ’BaseException’, ’Buffer
Error’, ’BytesWarning’, ’DeprecationWarning’, ’EOFError’, ’Ellipsis’, ’Environme
ntError’, ’Exception’, ’False’, ’FloatingPointError’, ’FutureWarning’, ’Generato
rExit’, ’IOError’, ’ImportError’, ’ImportWarning’, ’IndentationError’, ’IndexErr
or’, ’KeyError’, ’KeyboardInterrupt’, ’LookupError’, ’MemoryError’, ’NameError’,
’None’, ’NotImplemented’, ’NotImplementedError’, ’OSError’, ’OverflowError’, ’PendingDeprecationWarning’, ’ReferenceError’, ’RuntimeError’, ’RuntimeWarning’, ’
StopIteration’, ’SyntaxError’, ’SyntaxWarning’, ’SystemError’, ’SystemExit’, ’TabError’, ’True’, ’TypeError’, ’UnboundLocalError’, ’UnicodeDecodeError’, ’UnicodeEncodeError’, ’UnicodeError’, ’UnicodeTranslateError’, ’UnicodeWarning’, ’UserWarning’, ’ValueError’, ’Warning’, ’ZeroDivisionError’, ’__build_class__’, ’__debug__’, ’__doc__’, ’__import__’, ’__name__’, ’__package__’, ’abs’, ’all’, ’any’,’ascii’, ’bin’, ’bool’, ’bytearray’, ’bytes’, ’chr’, ’classmethod’, ’compile’, ’complex’, ’copyright’, ’credits’, ’delattr’, ’dict’, ’dir’, ’divmod’, ’enumerate’, ’eval’, ’exec’, ’exit’, ’filter’, ’float’, ’format’, ’frozenset’, ’getattr’,’globals’, ’hasattr’, ’hash’, ’help’, ’hex’, ’id’, ’input’, ’int’, ’isinstance’,’issubclass’, ’iter’, ’len’, ’license’, ’list’, ’locals’, ’map’, ’max’, ’memoryview’, ’min’, ’next’, ’object’, ’oct’, ’open’, ’ord’, ’pow’, ’print’, ’property’, ’quit’, ’range’, ’repr’, ’reversed’, ’round’, ’set’, ’setattr’, ’slice’, ’sorted’, ’staticmethod’, ’str’, ’sum’, ’super’, ’tuple’, ’type’, ’vars’, ’zip’]
这篇关于Python3.2官方文档--标准接口和dir()函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!