51Testing软件测试论坛

标题: Python小技巧 [打印本页]

作者: 测试积点老人    时间: 2018-12-19 11:52
标题: Python小技巧
使用
查看python的内置函数"BIF"

BIF内建函数可以自动导入到__main__命名空间,但是内建函数都有自己的命名空间——即__builtins__,因此我们可以通过Python shell,然后通过如下命令查看所有的BIF:
  1. <font size="2" style="font-weight: normal;">>>> dir(__builtins__)
  2. ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning',
  3. 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
  4. 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning',
  5. 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError',
  6. 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError',
  7. 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError',
  8. 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True',
  9. 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
  10. 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError',
  11. '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply',
  12. 'basestring', 'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'cmp', 'coerce',
  13. 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile',
  14. 'exit', 'file', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id',
  15. 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long', 'map', 'max',
  16. 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range',
  17. 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'static method',
  18. 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']</font>
复制代码

然后,我们可以通过help(<object>)命令查看BIF内建函数的详细信息:
  1. <font size="2" style="font-weight: normal;">>>> help(range)
  2. Help on built-in function range in module __builtin__:

  3. range(...)
  4. range(stop) -> list of integers
  5. range(start, stop[, step]) -> list of integers

  6. Return a list containing an arithmetic progression of integers.
  7. range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
  8. When step is given, it specifies the increment (or decrement).
  9. For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
  10. These are exactly the valid indices for a list of 4 elements.
  11. (END)</font>
复制代码
查看python的模块放置在计算机的什么地方打开python的命令行工具,

输入命令: >>> import sys; sys.path

输出如下(mac环境下):
  1. <span style="font-weight: normal;"><font size="2">['', '/Library/Python/2.7/site-packages/virtualenv-15.0.2-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']</font></span>
复制代码

Python的包管理器PyPI

PyPI是基于互联网的第三方Python模块存放的一个存储库。




作者: Miss_love    时间: 2021-1-5 17:01
支持分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2