python: nested classes: access outer class class member
<p>This does not work:</p>

<pre><code>class A:
  a1 = 4

  class B:
    b1 = A.a1  # Fails
    b2 = 6

  class C:
    c1 = A.B.b2  # Fails
</code></pre>

<p>Any non cryptic way to solve it? I know I could take B and C out  from A but I would like to keep them embedded. I also think it would be easier with no class members as they could easily passed to nested class as argument in constructor, but being all of them class members I do not know how to do some similar thing here. I have also read on some thread that this usage remembers using classes as namespaces and that should be solved using modules, not classes, but above classes are real classes for me (I construct instances) with class data in adition that I would like to share among them.</p>

<p>UPDATE: I found a really dirty way to share class members, I am not happy yet:</p>

<pre><code>global_tal = None                                    
global_tal2 = None                                   


class A:                                             
    a1 = 4                                           
    global global_tal                                
    global_tal = a1                                  

    class B:                                         
        global global_tal                            
        b0 = global_tal  # it works                  
        # Desired way: b1 = A.a1  # Fails            
        b2 = 6                                       

    global global_tal2                               
    global_tal2 = B                                  

    class C:                                         
        global global_tal2                           
        c1 = global_tal2.b2  # it works              
        # Desired way: c1 = A.B.b2  # Fails          
        pass                                         
</code></pre>

<p>UPDATE 2: Cleaner way (IMHO). Thanks to dhke.</p>

<pre><code>class _SharedReferences:                                     
      ref1 = None                                             
      ref2 = None                                             


class A(_SharedReferences):                                  
    a1 = 4                                                  
    _SharedReferences.ref1 = a1                              

    class B:                                                
        b0 = _SharedReferences.ref1  # it works              
        # Desired way: b1 = A.a1  # Fails                   
        b2 = 6                                              

    _SharedReferences.ref1 = B                               

    class C:                                                
        c1 = _SharedReferences.ref1.b2  # it works           
        # Desired way: c1 = A.B.b2  # Fails                 
        pass                                                
</code></pre>

<p>One trick is to put the common parameters into a parameter class and inherit from that:</p>

<pre><code>class Params:
   p = 4

class A(Params):
    # A has p
    class B(Params):
        # B has p
        pass

    class C(Params):
        # C has p
        pass
</code></pre>

<p>Or, if you need the params with different names in the inner classes:</p>

<pre><code>class A(Params):
    # A has p
    class B:
        b = Params.p

    class C:
        c = Params.p
</code></pre>

<p>This avoids having to monkey-patch the class after creation.</p>

python nested inner-classes