如何正确处理Python中的循环模块依赖关系? [英] How to properly handle a circular module dependency in Python?

查看:98
本文介绍了如何正确处理Python中的循环模块依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到一个合适的模式来处理Python中的循环模块依赖关系.通常,解决方案是将其删除(通过重构).但是,在这种情况下,我们真的很想拥有需要循环导入的功能.

Trying to find a good and proper pattern to handle a circular module dependency in Python. Usually, the solution is to remove it (through refactoring); however, in this particular case we would really like to have the functionality that requires the circular import.

编辑:根据以下答案,针对此类问题的通常攻击角度将是重构.但是,出于这个问题,假定这不是一个选择(无论出于何种原因).

EDIT: According to answers below, the usual angle of attack for this kind of issue would be a refactor. However, for the sake of this question, assume that is not an option (for whatever reason).

问题:

logging 模块需要一些配置数据的 configuration 模块.但是,对于某些 configuration 函数,我真的很想使用 logging 模块中定义的自定义日志记录功能.显然,在 configuration 中导入 logging 模块会引发错误.

The logging module requires the configuration module for some of its configuration data. However, for some of the configuration functions I would really like to use the custom logging functions that are defined in the logging module. Obviously, importing the logging module in configuration raises an error.

我们可以想到的可能解决方案:

  1. 不要这样做.如我之前所说,这不是一个好选择,除非所有其他可能性都很丑陋和糟糕.

  1. Don't do it. As I said before, this is not a good option, unless all other possibilities are ugly and bad.

对模块进行猴子修补.听起来还不错:在初始导入之后以及实际使用其任何功能之前,将日志模块动态加载到配置中.不过,这意味着要定义每个模块的全局变量.

Monkey-patch the module. This doesn't sound too bad: load the logging module dynamically into configuration after the initial import, and before any of its functions are actually used. This implies defining global, per-module variables, though.

依赖项注入.我已经阅读并遇到了依赖注入替代方案(尤其是在Java Enterprise领域),它们消除了一些麻烦.但是,它们可能过于复杂以致于无法使用和管理,这是我们希望避免的事情.不过,我不知道全景图在Python中的情况如何.

Dependency injection. I've read and run into dependency injection alternatives (particularly in the Java Enterprise space) and they remove some of this headache; however, they may be too complicated to use and manage, which is something we'd like to avoid. I'm not aware of how the panorama is about this in Python, though.

启用此功能的好方法是什么?

What is a good way to enable this functionality?

非常感谢!

推荐答案

如前所述,可能需要进行一些重构.根据名称,如果日志记录模块使用配置可能是可以的,当考虑配置中应该包含的内容时,会想到配置参数,然后会出现一个问题,为什么配置日志根本没有?

As already said, there's probably some refactoring needed. According to the names, it might be ok if a logging modules uses configuration, when thinking about what things should be in configuration one think about configuration parameters, then a question arises, why is that configuration logging at all?

可能是在配置中使用日志记录的代码部分不属于配置模块:似乎正在执行某种处理并记录结果或错误.

Chances are that the parts of the code under configuration that uses logging does not belong to the configuration module: seems like it is doing some kind of processing and logging either results or errors.

没有内在知识,仅使用常识,配置"模块应该是简单的事情,无需太多处理,并且应该是导入树中的叶子.

Without inner knowledge, and using only common sense, a "configuration" module should be something simple without much processing and it should be a leaf in the import tree.

希望有帮助!

这篇关于如何正确处理Python中的循环模块依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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