Python 模块变得太大
我的模块全部位于一个大文件中,该文件越来越难以维护。打破事物的标准方式是什么?
我的文件 my_module.py
中有一个模块,我像这样导入该模块:
import my_module
“my_module”很快就会有一千行,这突破了我保持一切正常的能力的极限。我正在考虑添加文件 my_module_base.py
、my_module_blah.py
等。然后,将 my_module.py
替换为
from my_module_base import *
from my_module_blah import *
# etc.
Then,用户代码不需要改变:
import my_module # still works...
这是标准模式吗?
My module is all in one big file that is getting hard to maintain. What is the standard way of breaking things up?
I have one module in a file my_module.py
, which I import like this:
import my_module
"my_module" will soon be a thousand lines, which is pushing the limits of my ability to keep everything straight. I was thinking of adding files my_module_base.py
, my_module_blah.py
, etc. And then, replacing my_module.py
with
from my_module_base import *
from my_module_blah import *
# etc.
Then, the user code does not need to change:
import my_module # still works...
Is this the standard pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您的模块实际在做什么。通常,将模块设置为一个包含“
__init__.py”
文件的目录总是一个好主意。因此,您首先要将your_module.py
转换为your_module/__init__.py
之类的内容。之后,您继续按照您的业务逻辑进行。这里有一些例子:
您是否有模块 API 不直接使用的实用程序函数,请将它们放入名为
utils.py
的文件中您是否有一些处理数据库或表示数据库模型的类,将它们放入
models.py
>你是否有一些内部配置,将其放入某些中可能是有意义的名为
settings.py
或config.py
的额外文件
这些只是示例(稍微借鉴了可重用应用程序的 Django 方法 ^^)。如前所述,这在很大程度上取决于您的模块的功能。如果之后它仍然太大,那么创建子模块也是有意义的(作为具有自己的
__init__.py
的子目录)。It depends on what your module is doing actually. Usually it is always a good idea to make your module a directory with an '
__init__.py'
file inside. So you would first transform youryour_module.py
to something likeyour_module/__init__.py
.After that you continue according to your business logic. Here some examples:
do you have utility functions which are not directly used by the modules API put them in some file called
utils.py
do you have some classes dealing with the database or representing your database models put them in
models.py
do you have some internal configuration it might make sense to put it into some extra file called
settings.py
orconfig.py
These are just examples (a little bit stolen from the Django approach of reusable apps ^^). As said, it depends a lot what your module does. If it is still too big afterwards it also makes sense to create submodules (as subdirectories with their own
__init__.py
).我确信对此有很多意见,但我想说你将其分解为更明确定义的功能单元(模块),包含在一个包中。然后你使用:
然后使用包名称来引用对象:
等等。
你应该(几乎)永远不要使用
因为这会引入错误(来自不同模块的重复名称最终会破坏一个)。
i'm sure there are lots of opinions on this, but I'd say you break it into more well-defined functional units (modules), contained in a package. Then you use:
Then use the package name to reference the object:
etc.
You should (almost) never use
Since that can introduce bugs (duplicate names from different modules will end up clobbering one).
不,这不是标准模式。
from Something import *
通常不是一个好的做法,因为它会导入很多你不打算导入的东西。相反,请遵循与您相同的方法,但将模块从一个模块专门包含到另一个模块,例如在
base.py
中,如果您有def myfunc
,则在main 中.py
使用from base import myfunc
这样对于您的用户来说,main.myfunc
也可以工作。当然,您需要注意不要最终进行循环导入。另外,如果您发现需要
from Something import *
,请使用__all__
构造控制导入值。No, that is not the standard pattern.
from something import *
is usually not a good practice as it will import lot of things you did not intend to. Instead follow the same approach as you did, but include the modules specifically from one to another for e.g.In
base.py
if you are havingdef myfunc
then inmain.py
usefrom base import myfunc
So that for your users,main.myfunc
would work too. Of course, you need to take care that you don't end up doing a circular import.Also, if you see that
from something import *
is required, then control the import values using the__all__
construct.