Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why nested scope rules do not apply to inner Class?

Reply
Thread Tools

Why nested scope rules do not apply to inner Class?

 
 
Cousson, Benoit
Guest
Posts: n/a
 
      08-12-2008
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

 
Reply With Quote
 
 
 
 
Salim Fadhley
Guest
Posts: n/a
 
      08-12-2008
> 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()


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Why nested scope rules do not apply to inner Class? Cousson, Benoit Python 3 08-14-2008 07:27 AM
Re: Why nested scope rules do not apply to inner Class? Calvin Spealman Python 5 08-14-2008 07:22 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
scope rules in nested functions Andrew Collier Python 0 01-29-2005 10:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments