在模块和/或包中组织 Python 类
我喜欢每个文件有一个公共类的 Java 约定,即使有时有充分的理由将多个公共类放入一个文件中。就我而言,我有同一接口的替代实现。但是,如果我将它们放入单独的文件中,导入语句中就会有多余的名称(或误导性的模块名称):
import someConverter.SomeConverter
而 someConverter
将是文件(和模块)名称,而 SomeConverter
类名。这对我来说看起来很不优雅。将所有替代类放入一个文件中将导致更有意义的导入语句:
import converters.SomeConverter
但我担心如果我将所有相关类放入单个模块文件中,文件会变得相当大。这里的 Python 最佳实践是什么?每个文件一个类是否异常?
I like the Java convention of having one public class per file, even if there are sometimes good reasons to put more than one public class into a single file. In my case I have alternative implementations of the same interface. But if I would place them into separate files, I'd have redundant names in the import statements (or misleading module names):
import someConverter.SomeConverter
whereas someConverter
would be the file (and module) name and SomeConverter
the class name. This looks pretty inelegant to me. To put all alternative classes into one file would lead to a more meaningful import statement:
import converters.SomeConverter
But I fear that the files become pretty large, if I put all related classes into a single module file. What is the Python best practise here? Is one class per file unusual?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很多都是个人喜好。使用 python 模块,您可以选择将每个类保留在单独的文件中,并且仍然允许导入转换器。SomeConverter(或从转换器导入SomeConverter)
您的文件结构可以看起来像这样:
然后在您的
__init__.py
文件中:A lot of it is personal preference. Using python modules, you do have the option to keep each class in a separate file and still allow for
import converters.SomeConverter
(orfrom converters import SomeConverter
)Your file structure could look something like this:
and then in your
__init__.py
file:Zach 的解决方案在 Python 3 上失败。这是一个固定的解决方案。
很多都是个人喜好。使用 python 模块,您可以选择将每个类保留在单独的文件中,并且仍然允许导入转换器。SomeConverter(或从转换器导入SomeConverter)
您的文件结构可以看起来像这样:
然后在您的
__init__.py
文件中:Zach's solution breaks on Python 3. Here is a fixed solution.
A lot of it is personal preference. Using python modules, you do have the option to keep each class in a separate file and still allow for
import converters.SomeConverter
(orfrom converters import SomeConverter
)Your file structure could look something like this:
and then in your
__init__.py
file:上面的解决方案很好,但是在
__init__.py
中导入模块的问题是,这会导致所有模块被加载两次(效率低下)。尝试在otherconverter.py
末尾添加打印语句并运行otherconverter.py
。 (您会看到 print 语句被执行了两次)我更喜欢以下内容。使用另一个名为“_converter”的包并定义其中的所有内容。然后你的“converters.py”成为访问所有公共成员的接口
,其中
converters.py
是正如前面提到的解决方案,这是个人选择。一些实践涉及在包中定义模块“interace.py”并在此处导入所有公共成员。如果您有许多模块要加载,您应该选择效率而不是美观。
The above solutions are good, but the problem with importing modules in
__init__.py
is that this will cause all the modules to be loaded twice(inefficient). Try adding a print statement at the end ofotherconverter.py
and runotherconverter.py
. (You'll see that the print statement is executed twice)I prefer the following. Use another package with name "_converter" and define everything there. And then your "converters.py" becomes the interface for accessing all public members
where
converters.py
isAnd as the previous solutions mentioned, it is a personal choice. A few practices involve defining a module "interace.py" within the package and importing all public members here. If you have many modules to load, you should choose efficiency over aesthetics.