On Wed, Dec 14, 2011 at 11:05 PM, Jean-Michel Pichavant <jeanmic...@sequans.com> wrote: > Chris Angelico wrote: >> >> So... it's a bad idea for me to use 'i' many times in my code, with >> the same name having different meanings in different places? In >> languages with infinitely-nesting scopes...
> Bad ideas : > > i = 5 > > def spam(): > for i,v in enumerate([1,2,3,4]): > for i,v in enumerate(['a','b', 'c']): > print i, v > print i,v # bad surprise That's my point. It's not safe to do it in Python, because the "inner" local i is the same as the "outer" local i. Python doesn't have block scope the way most C-like languages do. int spam() { for (int i=0;i<5;++i) { for (int i=2;i<4;++i) write("inner "+i+"\n"); write("outer "+i+"\n"); } } Works perfectly, and the only cost is that variables must be formally declared. In Python, you can kinda fudge that sort of thing with nested functions. def spam(): q=2 # just to prove that the scopes really are nested for i in range(5): def ham(): for i in range(2,4): print("q = %d, i = %d"%(q,i)) ham() print("i = %d"%i) It's somewhat clunky, but it does give the illusion of block scope. Inners mask outers, outers are visible to inner functions. It's an odd construct though. Very odd. So... I think I've figured out how to implement from __future__ import braces. #define { def innerfunc(): #define } innerfunc() And there you are, out of your difficulty at once! ChrisA -- http://mail.python.org/mailman/listinfo/python-list