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

发布于 2024-09-25 22:28:18 字数 427 浏览 3 评论 0原文

我喜欢每个文件有一个公共类的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦断已成空 2024-10-02 22:28:18

很多都是个人喜好。使用 python 模块,您可以选择将每个类保留在单独的文件中,并且仍然允许导入转换器。SomeConverter(或从转换器导入SomeConverter)

您的文件结构可以看起来像这样:

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

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

from baseconverter import BaseConverter
from otherconverter import OtherConverter

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)

Your file structure could look something like this:

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

and then in your __init__.py file:

from baseconverter import BaseConverter
from otherconverter import OtherConverter
吐个泡泡 2024-10-02 22:28:18

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

很多都是个人喜好。使用 python 模块,您可以选择将每个类保留在单独的文件中,并且仍然允许导入转换器。SomeConverter(或从转换器导入SomeConverter)

您的文件结构可以看起来像这样:

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

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

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

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 (or 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__.py file:

from converters.baseconverter import BaseConverter
from converters.otherconverter import OtherConverter
病毒体 2024-10-02 22:28:18

上面的解决方案很好,但是在__init__.py中导入模块的问题是,这会导致所有模块被加载两次(效率低下)。尝试在 otherconverter.py 末尾添加打印语句并运行 otherconverter.py。 (您会看到 print 语句被执行了两次)

我更喜欢以下内容。使用另一个名为“_converter”的包并定义其中的所有内容。然后你的“converters.py”成为访问所有公共成员的接口

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

*  converters.py

,其中 converters.py

from _converters.someconverter import SomeConverter
from _converters.otherconverter import OtherConverter
...
...
...
converters = [SomeConverter, OtherConverter, ...]

正如前面提到的解决方案,这是个人选择。一些实践涉及在包中定义模块“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 of otherconverter.py and run otherconverter.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

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

*  converters.py

where converters.py is

from _converters.someconverter import SomeConverter
from _converters.otherconverter import OtherConverter
...
...
...
converters = [SomeConverter, OtherConverter, ...]

And 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文