Incorrect scope of list comprehension variables

Duncan Booth duncan.booth at invalid.invalid
Mon Apr 19 05:05:11 EDT 2010


Dave Angel <davea at ieee.org> wrote:

> 2) In original C, and I think in C++, the lifetime of i lasted long 
> after the loop ended.
>      for (int i=0; i< limit; ++i)
>      {
>            z += i;
>      }
>       i is still valid after this curly brace
> 
> In C99, and at least in later C++, the scope of i ends with the curly, 
> as though there were another invisible pair of braces:
>      {
>      for (int i=0; i< limit; ++i)
>      {
>            z += i;
>      }}
>       i is no longer valid here
> 

Leading to the wonderful header declaration:

#define for if(0);else for

which moves the entire for loop including the declaration inside another 
statement and therefore 'fixes' the variable scope for older compilers.

Ah, those were the days. :^)

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list