Python中print(__doc__)作用

版权声明:本文为博主原创,转载请注明出处 https://blog.csdn.net/fireflylane/article/details/84575211

作用

输出文件开头注释的内容

具体用法

1)momodule.py

"""This is the module docstring."""

def f(x):
    """This is the function docstring."""
    return 2 * x

2)执行

>>> import mymodule
>>> mymodule.__doc__
'This is the module docstring.'
>>> mymodule.f.__doc__
'This is the function docstring.'

相关知识

  Python有个特性叫做文档字符串,即DocString,这个特性可以让你的程序文档更加清晰易懂,在python的系统文件里,也都在使用这个特性。因此推荐每个Python爱好者都使用它。
  DocString是有自己的格式的。一般是放在自己函数定义的第一行,用‘’符号指示,在这‘’里面添加函数功能说明就可以了。这个说明可以使用.doc(注意前后都是双_)属性,将DocString特性print打印出来。
  DocSting的典型用法就是help()调用,它抓取DocString属性,清晰的给你展示出来。

参考

print(doc) in Python 3 script

猜你喜欢

转载自blog.csdn.net/fireflylane/article/details/84575211