Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly handle a circular module dependency in Python?
    text
    copied!<p>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.</p> <p><strong>EDIT</strong>: 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).</p> <p><strong>The problem:</strong></p> <p>The <code>logging</code> module requires the <code>configuration</code> module for some of its configuration data. However, for some of the <code>configuration</code> functions I would really like to use the custom logging functions that are defined in the <code>logging</code> module. Obviously, importing the <code>logging</code> module in <code>configuration</code> raises an error.</p> <p><strong>The possible solutions we can think of:</strong></p> <ol> <li><p><strong>Don't do it</strong>. As I said before, this is not a good option, unless all other possibilities are ugly and bad.</p></li> <li><p><strong>Monkey-patch the module</strong>. This doesn't sound too bad: load the <code>logging</code> module dynamically into <code>configuration</code> <em>after</em> the initial import, and before any of its functions are actually used. This implies defining global, per-module variables, though.</p></li> <li><p><strong>Dependency injection</strong>. 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.</p></li> </ol> <p>What is a good way to enable this functionality?</p> <p>Thanks very much!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload