Python 代码样式-“;“扁平化”;一套';s名称空间

Python 代码样式-“;“扁平化”;一套';s名称空间,python,coding-style,namespaces,flatten,Python,Coding Style,Namespaces,Flatten,我的包层次结构: InstrumentController/ __init__.py instruments/ __init__.py _BaseInstrument.py Keithley2000.py # etc... 仪器文件的内容: # _BaseInstrument.py class _BaseInstrument(object): """Base class for instruments"""

我的包层次结构:

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...
我希望我的用户能够访问这些类,而不必深入研究模块的层次结构。他们只需从InstrumentController.instruments导入Keithley2000,而不是从InstrumentController.instruments.Keithley2000导入Keithley2000,输入

为此,我在
InstrumentController.instruments中有一系列类似的行。\uuu init\uuu

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

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

您的做法是可以接受的,但我建议您将所有包/模块名称改为小写,因为1)这是惯例,2)这将消除阴影问题。

将类放在单个模块中不是一个选项。将来,人们会编写新的文件放入包中,我不希望他们修改现有的文件。PEP8说,函数也应该有所有小写名称。建议使用什么命名约定来解决我需要从同名子模块导入函数的情况?通常的解决方法是不使用与模块同名的函数(这不是一件可怕的事情,因为模块和函数的名称都应该反映用途而不是所有权)。