Why nested scope rules do not apply to inner Class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cousson, Benoit

    Why nested scope rules do not apply to inner Class?

    Hi,

    I'd like to be able to use a nested class (C1) from another sibling nested class (C3). This looks very similar to the nested scopes of functions except that it does not work.

    class A(object):
    pass

    class B(object):

    class C1(object):
    pass

    class C2(C1):
    foo = A

    class C3(object):
    foo = C1

    The funny thing is that C2 can inherit from C1 but C3 cannot reference C1. B.C1 does not work either, but in that case it makes sense since B is stillbeing defined.
    Is this a language limitation or something that does not make sense at all?

    I'm wondering as well if the new nonlocal statement will fix that in py3k?

    Thanks in advance,
    Benoit

  • Salim Fadhley

    #2
    Re: Why nested scope rules do not apply to inner Class?

    I'm wondering as well if the new nonlocal statement will fix that in py3k?

    The "class C3" statement is executing before the "class B" statement
    has concluded, so at that time B does not exist in any scope at all,
    not even globals(). You could reference B.C1 inside a method because a
    method is executed AFTER the class is defined.
    class A(object):
    pass

    class B(object):

    class C1(object):
    pass

    class C2(C1):
    foo = A

    class C3(object):

    @staticmethod
    def test( ):
    print repr( B.C1 )
    print repr( B.C2 )

    B.C3.test()

    :-)

    Comment

    Working...