什么时候应该将 Python 脚本拆分为多个文件/模块?

发布于 2024-10-13 02:36:56 字数 197 浏览 2 评论 0原文

在 Java 中,这个问题很简单(虽然有点乏味)——每个类都需要自己的文件。因此,项目中 .java 文件的数量就是类的数量(不包括匿名/嵌套类)。

不过,在 Python 中,我可以在同一个文件中定义多个类,但我不太确定如何找到将它们分开的点。为每个类创建一个文件似乎是错误的,但默认情况下将所有内容都保留在同一个文件中也感觉是错误的。我如何知道在哪里分解程序?

In Java, this question is easy (if a little tedious) - every class requires its own file. So the number of .java files in a project is the number of classes (not counting anonymous/nested classes).

In Python, though, I can define multiple classes in the same file, and I'm not quite sure how to find the point at which I split things up. It seems wrong to make a file for every class, but it also feels wrong just to leave everything in the same file by default. How do I know where to break a program up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

五里雾 2024-10-20 02:36:56

请记住,在 Python 中,文件是一个模块,您很可能会导入该模块以便使用其中包含的类。还要记住软件开发的基本原则之一“打包的单位就是复用的单位” ,这基本上意味着:

如果类最有可能一起使用,或者如果使用一个类导致使用另一个类,那么它们属于一个公共包。

Remember that in Python, a file is a module that you will most likely import in order to use the classes contained therein. Also remember one of the basic principles of software development "the unit of packaging is the unit of reuse", which basically means:

If classes are most likely used together, or if using one class leads to using another, they belong in a common package.

寒尘 2024-10-20 02:36:56

在我看来,这实际上是一个关于重用和抽象的问题。如果您有一个可以用非常通用的方式解决的问题,以便生成的代码在许多其他程序中有用,请将其放在自己的模块中。

例如:不久前我写了一个(糟糕的)mpd 客户端。我想让配置文件和选项解析变得简单,所以我创建了一个类,以我认为合理的方式组合了 ConfigParser 和 optparse 功能。它需要几个支持类,因此我将它们全部放在一个模块中。我从来没有使用过客户端,但是我在其他项目中重用了配置模块。

编辑:另外,我刚刚想到了一个更愤世嫉俗的答案:如果你只能以一种非常丑陋的方式解决问题,请将丑陋隐藏在模块中。 :)

As I see it, this is really a question about reuse and abstraction. If you have a problem that you can solve in a very general way, so that the resulting code would be useful in many other programs, put it in its own module.

For example: a while ago I wrote a (bad) mpd client. I wanted to make configuration file and option parsing easy, so I created a class that combined ConfigParser and optparse functionality in a way I thought was sensible. It needed a couple of support classes, so I put them all together in a module. I never use the client, but I've reused the configuration module in other projects.

EDIT: Also, a more cynical answer just occurred to me: if you can only solve a problem in a really ugly way, hide the ugliness in a module. :)

海的爱人是光 2024-10-20 02:36:56

在 Java 中......每个类都需要自己的文件。

另一方面,有时 Java 文件也会在主类中包含枚举、子类或接口,因为它们“密切相关”。

不计算匿名/嵌套类

匿名类不应该被计算在内,但我认为有品味地使用嵌套类是一种选择,就像你问的关于Python的选择一样。

(有时一个 Java 文件会有两个类,不是嵌套的,这是允许的,但是不要这样做。)

In Java ... every class requires its own file.

On the flipside, sometimes a Java file, also, will include enums or subclasses or interfaces, within the main class because they are "closely related."

not counting anonymous/nested classes

Anonymous classes shouldn't be counted, but I think tasteful use of nested classes is a choice much like the one you're asking about Python.

(Occasionally a Java file will have two classes, not nested, which is allowed, but yuck don't do it.)

无畏 2024-10-20 02:36:56

Python 实际上让您可以选择以您认为合适的方式打包代码。

Python和Java之间的类比是,一个文件,即Python中的.py文件是
相当于Java中的包,因为它可以包含许多相关的类和函数。

要获得好的示例,请查看 Python 内置模块。
只需下载源代码并检查它们,我遵循的经验法则是
当你有非常紧密耦合的类或函数时,你可以将它们保存在一个文件中
否则你就拆散他们。

Python actually gives you the choice to package your code in the way you see fit.

The analogy between Python and Java is that a file i.e., the .py file in Python is
equivalent to a package in Java as in it can contain many related classes and functions.

For good examples, have a look in the Python built-in modules.
Just download the source and check them out, the rule of thumb I follow is
when you have very tightly coupled classes or functions you keep them in a single file
else you break them up.

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