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

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

问题描述

我来自一个背景,通常每个班级创建一个文件.我还组织目录下的通用类.这种做法对我来说很直观,并且已被证明在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类...,但是它还包含同步原语类,例如锁,事件,条件和信号量,以及可以通过线程操作引发的异常类. (还有其他一些东西).它处于合理大小的上限(包括空白和docstrings的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.

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

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