gpt4 book ai didi

python - 执行时如何打印 Python 文件的文档字符串?

转载 作者:IT老高 更新时间:2023-10-28 21:38:40 26 4
gpt4 key购买 nike

我有一个带有文档字符串的 Python 脚本。当命令行参数解析不成功时,我想打印用户信息的文档字符串。

有什么办法吗?

小例子

#!/usr/bin/env python
"""
Usage: script.py

This describes the script.
"""

import sys


if len(sys.argv) < 2:
print("<here comes the docstring>")

最佳答案

文档字符串存储在模块的 __doc__ 全局中。

print(__doc__)

顺便说一句,这适用于任何模块:import sys;打印(sys.__doc__)。函数和类的文档字符串也在它们的 __doc__ 属性中。

关于python - 执行时如何打印 Python 文件的文档字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7791574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com