【python3】查看模块及包含的方法

阅读 50

2022-02-18

  • dir:查看模块中的方法

    import sys
    print(dir(sys))
    ​
    输出:
    In [11]: print(dir(sys))
    ['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_framework', '_getframe', '_git', '_home', '_xoptions', 'abiflags', 'addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'ps3', 'pycache_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions']
  • help:查看模块或方法的内容

    In [12]: print(help(sys))
     
      
    # 也可以:
    In [13]: help()
    ​
    help> sys
    ​
    help> quit
    ​
    ​
    ​
    # 输出:
    Help on built-in module sys:
    ​
    NAME
        sys
    ​
    MODULE REFERENCE
        https://docs.python.org/3.8/library/sys
    ​
        The following documentation is automatically generated from the Python
        source files.  It may be incomplete, incorrect or include features that
        are considered implementation detail and may vary between Python
        implementations.  When in doubt, consult the module reference at the
        location listed above.
    ​
    DESCRIPTION
        This module provides access to some objects used or maintained by the
        interpreter and to functions that interact strongly with the interpreter.
    ​
        Dynamic objects:
    ​
        argv -- command line arguments; argv[0] is the script pathname if known
        path -- module search path; path[0] is the script directory, else ''
        modules -- dictionary of loaded modules
    ​
        displayhook -- called to show results in an interactive session
        excepthook -- called to handle any uncaught exception other than SystemExit
          To customize printing in an interactive session or to install a custom
          top-level exception handler, assign other functions to replace these.

  • 查看模块是内置还是外置

    In [7]: import requests
    ​
    In [8]: requests
    Out[8]: <module 'requests' from '/usr/lib/python3/dist-packages/requests/__init__.py'>
    ​
    In [9]: import sys
    ​
    In [10]: sys
    Out[10]: <module 'sys' (built-in)>

精彩评论(0)

0 0 举报