4

我的包层次结构:

InstrumentController/
    __init__.py
    instruments/
        __init__.py
        _BaseInstrument.py
        Keithley2000.py
        # etc...

仪器文件的内容:

# _BaseInstrument.py
class _BaseInstrument(object):
    """Base class for instruments"""
    # etc...

# Keithley2000.py
from InstrumentController.instruments._BaseInstrument import _BaseInstrument
class Keithley2000(_BaseInstrument):
    # etc...

我希望我的用户能够访问这些类,而不必深入研究模块的层次结构。他们应该只需要输入from InstrumentController.instruments import Keithley2000,而不是from InstrumentController.instruments.Keithley2000 import Keithley2000

为此,我有一堆这样的行InstrumentController.instruments.__init__

from .Keithley2000 import Keithley2000
from .StanfordSR830 import StanfordSR830
# etc...

所以现在这些类位于包命名空间的顶部,而不是子模块中。我的问题是:这是个好主意吗?这些类与它们所属的模块具有相同的名称,因此在顶层导入该类会使模块不可用。这让我有点神经质——有没有更好的方法呢?

4

1 回答 1

4

你的做法是可以接受的,但我建议你将所有包/模块名称重新转换为小写 1) 这是PEP 8 中指定的约定,以及 2) 它将消除你的阴影问题。

于 2012-09-27T17:01:56.007 回答