Python: Call for value inside nested class
<p>I'm trying to call for value from class B that is nested in class A and use it in class C.
I'm getting AttributeError:</p>

<pre><code>class A():
    class B():
        a = 1
    class C():
        b = 2
        c = B.a + b 
AttributeError: class B has no attribute 'a'
</code></pre>

<p>I also tried to call From 'A', Pycharm recognize it, but python still get AttributeError:</p>

<pre><code>class A(object):
    class B(object):
        a = 1
    class C(object):
        b = 2
        c = A.B.a + b 
AttributeError: class A has no attribute 'B'
</code></pre>

<p>Does someone have an idea of how to use it?
Thanks</p>

<p>You can not access the class by its name, while the class definition statement is still executed. </p>

<pre><code>class A(object):
   class B(object):
       a = 1
   class C(object):
       b = 2
       c = A.B.a + b # here class A statement is still executed, there is no A class yet
</code></pre>

<p>To solve the problem you must defer the execution of those statements :</p>

<ul>
<li>move the all those statements to a <a href="https://docs.python.org/3.5/library/functions.html?highlight=classmethod#classmethod" rel="nofollow">classmethod</a> </li>
<li>call them after the classes was defined.</li>
</ul>

<hr>

<pre><code>class A(object):
    class B(object):
        @classmethod
        def init(cls):
            cls.a = 1
    class C(object):
        @classmethod
        def init(cls):
            cls.b = 2
            cls.c = A.B.a + cls.b 

    @classmethod
    def init(cls):
        cls.B.init()
        cls.C.init()

A.init()
</code></pre>

python