I'm having trouble understanding scope of a variable in a subclass

Pyenos pyenos at pyenos.org
Thu Dec 28 01:31:40 EST 2006


Pyenos <pyenos at pyenos.org> writes:

> Thanks for clarifying the definitions of nested class and
> subclass. However, it did not solve my original problem, and I have
> redefined my question:
> 
> class Class1:
>     class Class2:
>         class Class3:
>             def __init__(self):
>                 self.var="var"
>             class Class4:
>                 print Class1.Class2.Class3.var
> 
> This code gives me the error:
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 2, in Class1
>   File "<stdin>", line 3, in Class2
>   File "<stdin>", line 6, in Class3
>   File "<stdin>", line 7, in Class4
> NameError: name 'Class1' is not defined
> 
> I have tried:
> 
> class Class1:
>         class Class2:
>                 def __init__(self):
>                         var="var"
> print Class1.Class2().var #this works
> 
> And, this worked. It is very strange that nested loop somehow fails to
> work when the innermost class has indentation level greater than two.

I found this link which is relevent:
http://mail.python.org/pipermail/python-list/2003-April/198978.html




More information about the Python-list mailing list