title: How to Handle Circularly Dependent Classes in Python
body: <p>I have two classes <code>Img</code> and <code>ImgAnalyzer</code> each in their own module. <code>ImgAnalyzer</code> accepts an <code>Img</code> object as a parameter and performs various analyses on it, but I want to have the <code>Img</code> object to 'own' the <code>ImgAnalyzer</code> object, ie if I want to use the analyzer, I need to use it through the image.</p>
  <p>ex:</p>
  <p><em>img.py</em></p>
  <pre><code>from img_analyzer import ImgAnalyzer
  class Img:
      def __init__(self, *args, **kwargs):
          # do thing
          self.analyzer = ImgAnalyzer(self)
      # other funcs
  </code></pre>
  <p><em>img_analyzer.py</em></p>
  <pre><code>from img import Img
  class ImgAnalyzer:
      def __init__(self, img: Image):
          # do thing
      # other funcs
  </code></pre>
  <p>Then I could access the analyzer from the image:</p>
  <pre><code>img = Img(params)
  img.analyzer.func()
  </code></pre>
  <p>This inevitably causes a circular import error.
  The classes should be in separate files because they have different functions and are too long to reasonably combine anyway. I looked around here and on other sites but couldn't figure out how to fix it.
  How can I have a class operated through and dependent on another class without causing errors?</p>