let me simplify my question on scope of vars

Gabriel Genellina gagsl-py at yahoo.com.ar
Fri Dec 22 20:38:35 EST 2006


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 




More information about the Python-list mailing list