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

WaterWalk toolmaster at 163.com
Thu Dec 28 20:14:34 EST 2006


Pyenos wrote:
> Approach 1:
>
> class Class1:
>         class Class2:
>                 def __init__(self):self.variable="variable"
>
>                 class Class3:
>                         def method():print Class1().Class2().variable #problem
>
> Approach 1.1:
>
> class Class1:
>         class Class2:
>                 def __init__(self):self.variable="variable"
>
>                 class Class3:
>                         def method():print Class1.Class2.variable #problem

> Approach 2:
>
> class Class1:
>         class Class2:
>                 variable="variable"
>
>                 class Class3:
>                         def method():print Class1().Class2().variable #problem
> Approach 2.1:
>
> class Class1:
>         class Class2:
>                 variable="variable"
>
>                 class Class3:
>                         def method():print Class1.Class2.variable #problem
>
> Is there a correct solution in the above? Or, what is the solution?

Your definition of Class3.method() shall have a 'self' argument, then
the above will all be ok.




More information about the Python-list mailing list