I'm having trouble understanding scope of a variable in a subclass
Gabriel Genellina
gagsl-py at yahoo.com.ar
Thu Dec 28 03:56:54 EST 2006
At Thursday 28/12/2006 03:18, Pyenos wrote:
>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
- put your print statement inside a method
- as I've said, try to grab the difference between an instance
attribute and a class attribute.
var is an attribute of Class3 instances (because you wrote self.var =
something), so you need a Class3 instance to access it.
Class3 is a class attribute of Class2. Class2 is an instance
attribute of Class1. Putting all this together, you can refer to such
"var" as Class1.Class2.Class3().var
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
More information about the Python-list
mailing list