【问题标题】:Is there any way of returning method documentation strings? [duplicate]有没有办法返回方法文档字符串? [复制]
【发布时间】:2018-06-03 06:42:15
【问题描述】:

由于 python3 中的 dir() 方法返回所有可用的可用方法, 是否有任何方法可以将方法的文档(说明)作为字符串返回?

如果是字典的 pop() 方法,它应该返回以下内容:

类 MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT])

@overload def pop 可能的类型:
• (self: MutableMapping, k: _KT) -> _VT

• (self: MutableMapping, k: _KT, default: Union[_VT, _T]) -> Union[_VT, _T]

外部文档: http://docs.python.org/3.6/library/

【问题讨论】:

  • 这不是help(dict.pop) 的输出。那输出是什么?
  • 哦,我在 pycharm 中按了 ^ + J 以获得方法文档
  • 那么...你想要pycharm的文档作为输出还是官方的?
  • 嗯实际上我认为 help(), (doc) 内置方法有效!

标签: python methods documentation


【解决方案1】:

您可以使用__doc__ 属性访问函数的文档字符串:

➜ python
Python 3.6.4 (default, May 23 2018, 17:30:17)
Type "help", "copyright", "credits" or "license" for more information.
>>> print(dict.pop.__doc__)
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised

【讨论】:

    【解决方案2】:

    [已解决]

    正如提到的 cmets 和答案,help() 或 doc 都可以正常工作!

    print(help(dict.pop))
    
    print(dict.pop.__doc__)
    

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 1970-01-01
      • 2020-11-02
      • 2020-01-24
      • 2013-09-20
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2016-10-13
      相关资源
      最近更新 更多