51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2107|回复: 1
打印 上一主题 下一主题

Python小技巧

[复制链接]
  • TA的每日心情
    无聊
    前天 09:11
  • 签到天数: 406 天

    连续签到: 3 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2018-12-19 11:52:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    使用
    查看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模块存放的一个存储库。



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-2 05:50 , Processed in 0.060613 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表