From: Stephen Hansen on
On 2010-04-03 23:30:32 -0700, Steve Howell said:

> On Apr 3, 9:58�pm, Tim Roberts <t...(a)probo.com> wrote:
>> Alain Ketterlin <al...(a)dpt-info.u-strasbg.fr> wrote:
>>
>>> 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
>>
>> Yes, this has been fixed in later revisions, but I'm curious to know what
>> led you to believe that a list comprehension created a new scope. �I don't
>> that was ever promised.
>
> Common sense about how programming languages should work? As
> confirmed by later revisions?

Where exactly does this common sense come from? A list comprehension is
basically syntactic sugar over a for loop, and...

Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> for x in range(10):
pass

>>> x
9


--
--S

.... p.s: change the ".invalid" to ".com" in email address to reply privately.

From: Ethan Furman on
Steve Howell wrote:
> On Apr 3, 9:58 pm, Tim Roberts <t...(a)probo.com> wrote:
>
>>Alain Ketterlin <al...(a)dpt-info.u-strasbg.fr> wrote:
>>
>>
>>>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
>>
>>Yes, this has been fixed in later revisions, but I'm curious to know what
>>led you to believe that a list comprehension created a new scope. I don't
>>that was ever promised.
>
>
> Common sense about how programming languages should work? As
> confirmed by later revisions?

Common sense? About *somebody else's* idea of how a programming
language should work?

Please. Experiment and read the manual.

~Ethan~
From: Alf P. Steinbach on
* Ethan Furman:
> Steve Howell wrote:
>> On Apr 3, 9:58 pm, Tim Roberts <t...(a)probo.com> wrote:
>>
>>> Alain Ketterlin <al...(a)dpt-info.u-strasbg.fr> wrote:
>>>
>>>
>>>> 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
>>>
>>> Yes, this has been fixed in later revisions, but I'm curious to know
>>> what
>>> led you to believe that a list comprehension created a new scope. I
>>> don't
>>> that was ever promised.
>>
>>
>> Common sense about how programming languages should work? As
>> confirmed by later revisions?
>
> Common sense? About *somebody else's* idea of how a programming
> language should work?

Common sense is about practical solutions.

Since there is no practical gain from a list comprehension affecting the
bindings of outside variables, and there correspondingly is a practical pay-off
from list comprehensions not affecting the bindings of outside variables, common
sense is to expect the latter.

It's in the nature of common sense that those who possess this ability often
tend to make the same tentative assumptions when presented with the same
problem. It doesn't mean that they're consulting each other, like your "somebody
else's": it just means that they're applying similar common sense reasoning. So,
there's no great conspiracy.


> Please. Experiment and read the manual.

Common sense is applied first, as a heuristic. You really wouldn't want to drill
down into the architect's drawings in order to get office 215 in a building.
First you apply common sense.



Cheers & hth.,

- Alf
From: D'Arcy J.M. Cain on
On Sun, 04 Apr 2010 15:06:46 +0200
"Alf P. Steinbach" <alfps(a)start.no> wrote:
> Common sense is applied first, as a heuristic. You really wouldn't want to drill
> down into the architect's drawings in order to get office 215 in a building.
> First you apply common sense.

Oh goodie, bad analogies. Can I play too?

Getting to office 215 is not analogous to writing a program. It is
analogous to using the program. Writing the program is like building
the office tower. You need to know about the tools and materials that
you are working with. You don't use "common sense" to decide what
materials to use. You study the literature and the specs.

--
D'Arcy J.M. Cain <darcy(a)druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
From: Paul Rubin on
Alain Ketterlin <alain(a)dpt-info.u-strasbg.fr> writes:
> d[r] = [r for r in [4,5,6]]
> 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.

Yes, this is a well known design error in Python 2.x. The 3.x series
fixes this error but introduces other errors of its own. It is evil
enough that I almost always use this syntax instead:

d[r] = list(r for r in [4,5,6])

that works in 3.x and the later releases of 2.x. In early 2.x (maybe up
to 2.2) it throws an error at compile time rather than at run time.