Why nested scope rules do not apply to inner Class?

C

Cousson, Benoit

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
 
S

Salim Fadhley

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()

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top