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

本文介绍了如何在Python3中使用`dir()`函数查看模块及其包含的方法,并通过`help()`获取模块或方法的详细信息。示例中展示了`sys`模块的例子,展示如何查看其内置属性和方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 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)>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MGonster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值