Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a more concise, minimal example to reproduce your problem:</p> <p>a.py</p> <pre class="lang-python prettyprint-override"><code>import b class A(object): def get_magic_number_from_b(self): return b.magic_number() </code></pre> <p>b.py</p> <pre class="lang-python prettyprint-override"><code>import a def magic_number(): return 42 class B(a.A): pass </code></pre> <p>Similar to your example, class B in module b inherits from class A in module a. At the same time, class A needs some functionality from module b to perform its function (in general, you should try to avoid this if you can). Now, when you import module a, Python will import module b as well. This fails with an <code>AttributeError</code> exception since the class b.B depends explicitly on a.A, which is not yet defined at the time when the <code>import b</code> statement is executed.</p> <p>To solve this issue, you can either move the <code>import b</code> statement behind the definition of A, like this:</p> <pre><code>class A(object): def get_magic_number_from_b(self): return b.magic_number() import b </code></pre> <p>, or you move it to within the definition of the function that depends on the functionality from module b, like this:</p> <pre><code>class A(object): def get_magic_number_from_b(self): import b return b.magic_number() </code></pre> <p>Alternatively, you can make sure that you always import module <code>b</code> before module <code>a</code>, which will also solve the problem (since a has no import-time dependencies on b).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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