From: Aahz on
In article <4bb92850$0$8827$c3e8da3(a)news.astraweb.com>,
Steven D'Aprano <steve(a)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.
--
Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
From: Raymond Hettinger on
On Apr 3, 3:30 am, Alain Ketterlin <al...(a)dpt-info.u-strasbg.fr>
wrote:
> Hi all,
>
> I've just spent a few hours debugging code similar to this:
>
> d = dict()
> for r in [1,2,3]:
>     d[r] = [r for r in [4,5,6]]
> print d
>
> THe problem is that the "r" in d[r] somehow captures the value of the
> "r" in the list comprehension, and somehow kills the loop interator. The
> (unexpected) result is {6: [4, 5, 6]}. Changing r to s inside the list
> leads to the correct (imo) result.
>
> Is this expected? Is this a known problem? Is it solved in newer
> versions?

It is the intended behavior in 2.x. The theory was that a list
comprehension would have the same effect as if it had been unrolled
into a regular for-loop.

In 3.x, Guido changed his mind and the induction variable is hidden.
The theory is that some folks (like you) expect the variable to be
private and that is what we already do with generator expressions.

There's no RightAnswer(tm), just our best guess as to what is the most
useful behavior for the most number of people.

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

> In article <4bb92850$0$8827$c3e8da3(a)news.astraweb.com>, Steven D'Aprano
> <steve(a)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...


--
Steven
From: Chris Rebert on
On Fri, Apr 16, 2010 at 5:20 PM, Steven D'Aprano
<steve(a)remove-this-cybersource.com.au> wrote:
> On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote:
>> In article <4bb92850$0$8827$c3e8da3(a)news.astraweb.com>, Steven D'Aprano
>> <steve(a)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...

Well, technically it's the idiomatic placement of the loop variable
declaration rather than the loop construct itself, but:

//Written in Java
//Can trivially be changed to C99 or C++
for (int i = 0; i < array.length; i++)
{
// code
}
// variable 'i' no longer accessible

//Using a for-each loop specific to Java
for (ItemType item : array)
{
// code
}
// variable 'item' no longer accessible

Cheers,
Chris
--
http://blog.rebertia.com
From: MRAB on
Steven D'Aprano wrote:
> On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote:
>
>> In article <4bb92850$0$8827$c3e8da3(a)news.astraweb.com>, Steven D'Aprano
>> <steve(a)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...
>
The programming language Ada comes to mind (the variable exists only
within the body of the loop and is read-only like a constant), so yes,
that's just your ignorance. ;-)