Python:更喜欢几个小模块还是一个更大的模块? [英] Python: prefer several small modules or one larger module?

查看:108
本文介绍了Python:更喜欢几个小模块还是一个更大的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Python Web应用程序,其中有一些小模块可以提供非常特定的功能:session.pylogger.pydatabase.py等.这些文件中的每一个目前都包含大约3-5行代码,或者最多最多10行.我可能有几个导入,每个都有一个或两个类定义.我想知道,我是否应该将这些内容合并为一个模块,例如misc.py?

I'm working on a Python web application in which I have some small modules that serve very specific functions: session.py, logger.py, database.py, etc. And by "small" I really do mean small; each of these files currently includes around 3-5 lines of code, or maybe up to 10 at most. I might have a few imports and a class definition or two in each. I'm wondering, is there any reason I should or shouldn't merge these into one module, something like misc.py?

我的想法是,使用单独的模块有助于提高代码的清晰度,以后,如果这些模块有机会增长到10行以上,那么我对分离它们不会感到很糟糕.但是,另一方面,一堆文件中只有几行似乎太浪费了!多文件方法与单文件方法之间在资源使用方面是否有显着差异? (当然,我离应该担心资源使用的地方不远,但是我忍不住要问...)

My thoughts are that having separate modules helps with code clarity, and later on, if by some chance these modules grow to more than 10 lines, I won't feel so bad about having them separated. But on the other hand, it just seems like such a waste to have a bunch of files with only a few lines in each! And is there any significant difference in resource usage between the multi-file vs. single-file approach? (Of course I'm nowhere near the point where I should be worrying about resource usage, but I couldn't resist asking...)

我四处看看是否曾经有人问过这个问题,并且没有看到Python特有的任何内容,但是如果它实际上是重复的,我将为您指出正确的方向而感激.

I checked around to see whether this had been asked before and didn't see anything specific to Python, but if it's in fact a duplicate, I'd appreciate being pointed in the right direction.

推荐答案

我的想法是分开 模块有助于代码清晰,以及 以后,如果有这些机会 模块增长到十多条线,我 拥有它们不会感到难过 分开.

My thoughts are that having separate modules helps with code clarity, and later on, if by some chance these modules grow to more than 10 lines, I won't feel so bad about having them separated.

这个.保持它有自己的方式.

This. Keep it the way you have it.

这篇关于Python:更喜欢几个小模块还是一个更大的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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