Why nested scope rules do not apply to inner Class?

Calvin Spealman ironfroggy at gmail.com
Tue Aug 12 15:51:30 CEST 2008


The simple answer is "Dont nest classes. It is not supported."

What you are seeing is really an artifact of how classes are built.
Basically, everything inside the class body has to exist before it can
run, so the inner classes code objects are actually created first.
However, the class object itself isnt created out of that until the
body of the outer class is evaluated.

On Tue, Aug 12, 2008 at 5:29 AM, Cousson, Benoit <b-cousson at ti.com> wrote:
> 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 still being 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/



More information about the Python-list mailing list