简体   繁体   English

Python:如何在包级别帮助菜单中嵌入所有docstring帮助?

[英]Python: how to embed all docstring help at package level help menu?

What I mean to ask is: 我的意思是:

  • TLDR: how do I have my package's help include all underlying docstrings? TLDR: 我如何让我的软件包的帮助包括所有基础文档字符串?
  • I have created a package. 我创建了一个包。 That package has all the proper __init__.py files and all the proper docstrings (module, function, class, and method level docstrings). 该包具有所有正确的__init__.py文件和所有正确的文档字符串(模块,函数,类和方法级文档字符串)。 However, when I perform help(mypackage) , the only help provided is the help provided at that top level __init__.py module. 但是,当我执行help(mypackage) ,唯一提供的帮助是在顶级__init__.py模块中提供的帮助。

Often package-level help does not include all of the underlying docstrings, but sometimes it does . 包级帮助通常不包括所有底层文档字符串,但有时它会包含所有底层文档字符串。

I want to make sure that I am embedding all of the underlying docstrings. 我想确保我嵌入了所有底层文档字符串。


For instance, within the numpy package all underlying docstrings are available in the help at the command prompt, even though they are not provided at the top-level __init__.py . 例如,在numpy中,命令提示符下的帮助中提供了所有基础文档字符串,即使它们未在顶级__init__.py

Ie, I can type 即,我可以输入

>>> help(numpy)

and see all of the documentation, including documentation defined outside of the dunder init module. 并查看所有文档,包括在dunder init模块之外定义的文档。

However, many other packages, including popular ones like the pandas package do not capture all of the underlying documentation. 但是,许多其他软件包,包括像pandas软件包这样的流行软件包,并没有捕获所有底层文档。

Ie, typing 即,打字

>>> help(pandas)

only provides me the documentation defined in __init__.py . 只提供了__init__.py定义的文档。

I want to create package-level documentation mirroring how numpy does it. 我想创建一个包级级文档来反映numpy是如何做到的。


I have tried to look through numpy to see how it is performing this magic, with no luck. 我试图通过numpy来看看它是如何表现这种魔力的,没有运气。 I have performed Google searches, but it seems there is no way to phrase this question and get any decent links back. 我已经进行了谷歌搜索,但似乎没有办法说出这个问题,并得到任何体面的链接。

numpy shows you documentation on classes and functions defined outside __init__.py module because of adding their names to __all__ variable in __init__.py . numpy向您展示了__init__.py模块之外定义的类和函数的文档,因为它们将名称添加到__init__.py __all__变量。 Try to comment lines 169-173 (don't forget to uncomment!): 尝试评论第169-173行(不要忘记取消注释!):

#__all__.extend(['__version__', 'show_config'])
#__all__.extend(core.__all__)
#__all__.extend(_mat.__all__)
#__all__.extend(lib.__all__)
#__all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])

After doing this output of help(numpy) will be very limited. 这样做后, help(numpy)输出将非常有限。

Also let's reproduce this behaviour. 还让我们重现这种行为。 Starting from '/some/path' , create folder folder , file named file.py inside it with the following content: '/some/path' ,使用以下内容创建文件folder文件folder ,文件名为file.py

class Class:
    """Class docstring"""

And __init__.py : __init__.py

from .file import *

Now let's see the help: 现在让我们看看帮助:

/some/path$ python3.5
>>> import folder
>>> help(folder)

Help on package folder:

NAME
    folder

PACKAGE CONTENTS
    file

FILE
    /some/path/folder/__init__.py

And now add this line to __init__.py : 现在将此行添加到__init__.py

__all__ = ['Class']

After reimporting folder the command help(folder) will contain information about class Class which includes your docstring: 重新导入folder ,命令help(folder)将包含有关包含docstring的class Class信息:

Help on package folder:

NAME
    folder

PACKAGE CONTENTS
    file

CLASSES
    builtins.object
        folder.file.Class

    class Class(builtins.object)
     |  Class docstring
     |  
     |  Data descriptors defined here:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)

DATA
    __all__ = ['Class']

FILE
    /some/path/folder/__init__.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM