组织模块和包的 Pythonic 方式 [英] The Pythonic way of organizing modules and packages

查看:16
本文介绍了组织模块和包的 Pythonic 方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的背景是我通常为每个班级创建一个文件.我也在目录下组织公共类.这种做法对我来说很直观,并且在 C++、PHP、JavaSript 等中被证明是有效的.

I come from a background where I normally create one file per class. I organize common classes under directories as well. This practice is intuitive to me and it has been proven to be effective in C++, PHP, JavaSript, etc.

我在将这个比喻引入 Python 时遇到了麻烦:文件不再只是文件,而是正式的模块.在一个模块中只包含一个类似乎是不正确的——大多数类本身是无用的.如果我有一个 automobile.py 和一个 Automobile 类,那么总是将它引用为 automobile.Automobile 似乎很愚蠢.

I am having trouble bringing this metaphor into Python: files are not just files anymore, but they are formal modules. It doesn't seem right to just have one class in a module --- most classes are useless by themselves. If I have a automobile.py and an Automobile class, it seems silly to always reference it as automobile.Automobile as well.

但是,与此同时,将大量代码放入一个文件并收工似乎也不合适.显然,一个非常复杂的应用程序应该有 5 个以上的文件.

But, at the same time, it doesn't seem right to throw a ton of code into one file and call it a day. Obviously, a very complex application should have more than 5 files.

正确的---或pythonic---方式是什么?(或者,如果没有正确的方法,您的首选方法是什么?为什么?)我应该在 Python 模块中投入多少代码?

What is the correct---or pythonic---way? (Or if there is no correct way, what is your preferred way and why?) How much code should I be throwing in a Python module?

推荐答案

从包装的逻辑单元"的角度思考——这可能是一个单一的类,但更多时候是一组紧密合作的类.类(或模块级函数——当模块级函数也可以作为一种选择时,不要通过总是使用静态方法在 Python 中执行 Java"!-)可以基于这个标准进行分组.基本上,如果 A 的大多数用户也需要 B,反之亦然,那么 A 和 B 应该在同一个模块中;但是如果许多用户只需要其中一个而不需要另一个,那么它们可能应该在不同的模块中(可能在同一个包中,即包含 __init__.py 文件的目录).

Think in terms of a "logical unit of packaging" -- which may be a single class, but more often will be a set of classes that closely cooperate. Classes (or module-level functions -- don't "do Java in Python" by always using static methods when module-level functions are also available as a choice!-) can be grouped based on this criterion. Basically, if most users of A also need B and vice versa, A and B should probably be in the same module; but if many users will only need one of them and not the other, then they should probably be in distinct modules (perhaps in the same package, i.e., directory with an __init__.py file in it).

标准 Python 库虽然远非完美,但往往反映(大部分)合理的良好实践——因此您可以通过示例从中学习.例如,threading 模块当然定义了一个 Thread 类......但它也包含同步原语类,例如锁、事件、条件和信号量,以及一个可以通过线程操作(以及其他一些事情)引发的异常类.它处于合理大小的上限(800 行,包括空格和文档字符串),并且一些关键的线程相关功能(例如 Queue)已放置在单独的模块中,但它是一个很好的示例,说明它仍然有意义的最大功能量打包成一个模块.

The standard Python library, while far from perfect, tends to reflect (mostly) reasonably good practices -- so you can mostly learn from it by example. E.g., the threading module of course defines a Thread class... but it also holds the synchronization-primitive classes such as locks, events, conditions, and semaphores, and an exception-class that can be raised by threading operations (and a few more things). It's at the upper bound of reasonable size (800 lines including whitespace and docstrings), and some crucial thread-related functionality such as Queue has been placed in a separate module, nevertheless it's a good example of what maximum amount of functionality it still makes sense to pack into a single module.

这篇关于组织模块和包的 Pythonic 方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆