在模块和/或包中组织 Python 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3842616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:58:43  来源:igfitidea点击:

Organizing Python classes in modules and/or packages

pythonclassmodulepackage

提问by deamon

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):

我喜欢每个文件有一个公共类的 Java 约定,即使有时有充分的理由将多个公共类放入单个文件中。就我而言,我有相同接口的替代实现。但是,如果我将它们放入单独的文件中,我会在导入语句(或误导性的模块名称)中有多余的名称:

import someConverter.SomeConverter

whereas someConverterwould be the file (and module) name and SomeConverterthe class name. This looks pretty inelegant to me. To put all alternative classes into one file would lead to a more meaningful import statement:

someConverter将是文件(和模块)名称和SomeConverter类名称。这对我来说看起来很不优雅。将所有替代类放在一个文件中会导致更有意义的导入语句:

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?

但是我担心如果我将所有相关的类放在一个模块文件中,文件会变得非常大。这里的 Python 最佳实践是什么?每个文件一个班级不寻常吗?

采纳答案by Zach

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(or from converters import SomeConverter)

很多都是个人喜好。使用 python 模块,您可以选择将每个类保存在一个单独的文件中,并且仍然允许import converters.SomeConverter(或from converters import SomeConverter)

Your file structure could look something like this:

您的文件结构可能如下所示:

* converters
     - __init__.py
     - baseconverter.py
     - someconverter.py
     - otherconverter.py

and then in your __init__.pyfile:

然后在您的__init__.py文件中:

from baseconverter import BaseConverter
from otherconverter import OtherConverter

回答by Spundun

Zach's solution breaks on Python 3. Here is a fixed solution.

Zach 的解决方案在 Python 3 上中断。这是一个固定的解决方案。

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(or from converters import SomeConverter)

很多都是个人喜好。使用 python 模块,您可以选择将每个类保存在一个单独的文件中,并且仍然允许import converters.SomeConverter(或from converters import SomeConverter)

Your file structure could look something like this:

您的文件结构可能如下所示:

* converters
     - __init__.py
     - baseconverter.py
     - someconverter.py
     - otherconverter.py

and then in your __init__.pyfile:

然后在您的__init__.py文件中:

from converters.baseconverter import BaseConverter
from converters.otherconverter import OtherConverter