Gabriel Genellina <[EMAIL PROTECTED]> writes:

> At Friday 22/12/2006 22:24, Pyenos wrote:
> 
> > > > "code"
> > > > var=1
> > > > class CLASS:
> > > >         def METHOD1:
> > > >                 def METHOD2:
> > > >                         var+=var
> > > >                 return var
> > > >                 METHOD2()       #line8
> > > >         return var
> > > > METHOD1()                       #line10
> > > > "end code"
> > > >
> > > > Q1: does class CLASS inherit var=0 from line1?
> > > yes.
> > > > Q2: does def METHOD1 inherit var=0 from line1?
> > > no.
> > > > Q3: does def METHOD2 inherit var=0 from line1?
> > > no.
> > > > Q3: does line8 return '2'?
> > > no. will get unreferenced var error.
> > > > Q4: does line10 return '2\n2'?
> > > no. will get unreferenced var error.
> >
> >Now I know that Q1 is also no, since var=1 from line 2 is a global
> >variable and I have not declared it as global inside def METHOD2. so
> >var within def METHOD2 is a different variable to the global variable var.
> 
> Read the Python Pitfalls I've send some minutes ago, and the tutorial
> (specially http://docs.python.org/tut/node11.html#scopes) and then
> re-answer your own questions.
> 
> 
> -- 
> 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

thanks for the helpful links. after consideration i think the code
should be more sensible if it is written in this way:

"code"
var=1
def METHOD1():
        METHOD2():
                global var
                var+=var
"end code"

so that it is more clear which var it is using, which in this case
should be from global var and not local var.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to