[Tutor] scope/namespaces

Alan Gauld alan.gauld at btinternet.com
Tue Apr 24 19:22:17 CEST 2007


"ammar azif" <ceasar102 at yahoo.com> wrote 

> Something in python disturbs me ,
> 
> when i write a for loop, i am able to access the variable 
> declared in that loop after the loop finishes which i am 
> not able to do in languages like c/c++ or java. 

That's a very recent change to C/C++ (1999 apparently), 
you used to be able to do that. In fact I hadn't realised that 
you couldn't any more! It was only when I wrote a test 
program I discovered you were right...

> Is it different in python?

Yes. Once you create a name within a scope it stays 
there and loops or code blocks are not a separate scope 
in Python 

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list