简体   繁体   English

如何在__init__.py中使用import *将docstrings引入包范围?

[英]How to bring docstrings into package scope with import * in __init__.py?

I have a Python package in which the implementation is split (for maintainability) into two internal submodules. 我有一个Python包,其中的实现被拆分(可维护性)为两个内部子模块。 From the user point of view the package should appear as one unit, though, so in the package's __init__.py both submodules are imported with import * , as follows: 从用户的角度来看,包应该显示为一个单元,因此在包的__init__.py两个子模块都使用import * ,如下所示:

# filesystem layout:
mypkg/
    __init__.py
    subA.py # defines class A
    subB.py # defines class B

and

# __init__.py
from .subA import *
from .subB import *

This works as intended from the package functionality point of view: 从包功能的角度来看,这是按预期工作的:

>>> import mypkg
>>> a = mypkg.A() # works
>>> b = mypkg.B() # works

and if looking up inline help for these classes directly, everything is also good: 如果直接查找这些类的内联帮助,一切都很好:

>>> help(mypkg.A) # works
>>> help(mypkg.subA.A) # also works

The problem is that if I just look up the help for the top-level package, cf. 问题是,如果我只是查找顶级包的帮助,请参阅

>>> help(mypkg)

then the classes and functions from the submodules do not "voluntarily" appear at all (although variables from them do appear in the DATA section). 那么子模块中的类和函数根本不会“自愿”出现(尽管它们的变量确实出现在DATA部分中)。 Is this expected/correct behaviour, and is there a way to bypass it so that the users do not have to know about the submodules that exist for implementation/maintenance convenience only? 这是预期/正确的行为,是否有办法绕过它,以便用户不必了解仅为实现/维护方便而存在的子模块?

我所知道的最佳解决方案是将相关的文档对象(类,函数,数据)添加到__init__.py __all__

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

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