我的包层次结构:
InstrumentController/
__init__.py
instruments/
__init__.py
_BaseInstrument.py
Keithley2000.py
# etc...
仪器文件的内容:
^{pr2}$我希望我的用户能够访问这些类,而不必深入研究模块的层次结构。他们只需要输入from InstrumentController.instruments import Keithley2000
,而不是{
为此,我在InstrumentController.instruments.__init__
中有一堆类似这样的行:
from .Keithley2000 import Keithley2000
from .StanfordSR830 import StanfordSR830
# etc...
所以现在类位于包名称空间的顶部,而不是子模块中。我的问题是:这是个好主意吗?这些类与它们所属的模块具有相同的名称,因此在顶层导入类会导致模块不可用。这让我有点紧张-有更好的方法吗?在
您的操作方式是可以接受的,但我建议您将所有的包/模块名称改为小写:1)这是惯例specified in PEP 8,2)它将消除隐藏问题。在
相关问题 更多 >
编程相关推荐