* Steven D'Aprano:
On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote:

In article <4bb92850$0$8827$c3e8...@news.astraweb.com>, Steven D'Aprano <st...@remove-this-cybersource.com.au> wrote:
Nevertheless, it is a common intuition that the list comp variable
should *not* be exposed outside of the list comp, and that the for-loop
variable should. Perhaps it makes no sense, but it is very common --
I've never heard of anyone being surprised that the for-loop variable is
exposed, but I've seen many people surprised by the fact that list-comps
do expose their loop variable.
I've definitely seen people surprised by the for-loop behavior.

What programming languages were they used to (if any)?

I don't know of any language that creates a new scope for loop variables, but perhaps that's just my ignorance...

MRAB has mentioned Ada, let me mention C++ ...


<code language="C++">
    #include <assert.h>

    int main()
    {
        int const   i = 42;

        for( int i = 0; i < 10; ++i )
        {
            // blah blah
        }
        assert( i == 42 );
    }
</code>


Java and C# take a slightly different approach where code analogous to the above won't compile. But it's still a nested scope. E.g. ...


<code language="Java">

    class App
    {
        static public void main( String[] args )
        {
            for( int i = 0; i < 10; ++i )
            {
                // blah blah
            }

            // Uncomment statement below to get compilation error:
            //System.out.println( i );
        }
    }
</code>


So, yes, considering Ada, C++, Java and C#  --  and so on. ;-)


Cheers & hth.,

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

Reply via email to