List comprehensions

Jesse Sweeney jesse at student.usyd.edu.au
Thu Dec 23 01:44:38 EST 1999


On Wed, 22 Dec 1999 10:51:39 GMT, thantos at chancel.org (Alexander Williams)
wrote:

>On 22 Dec 1999 10:28:52 GMT, Albert Hofkamp <hat at se-46.wpa.wtb.tue.nl>
>wrote:

[ snip ]

>> Another matter is scoping. I hope that code like
>> i := 30
>> ys := [ 4 ]
>> xs := [ i ] + [ i+15, for i in ys ] + [ i+1 ]
>>
>>is well-defined.
>>I'd like to have xs == [ 30, 19, 31 ] at the end, but I am too new to
>>Python to say anything useful about scoping.
>>Is this what you'd expect, and is it feasible ?
>
>Certainly; I don't expect scoping in the LCs will be much of an
>issue.  The above mock-up should be completely defined under present
>scoping rules.

Well, I haven't actually installed the patch in question, but as I understand it
the comprehension-using code above would translate to the following current
Python:

    i = 30
    ys = [4]
    xs = [i]
    for i in ys:
        xs = xs + [i+15]
    xs = xs + [i+1]

which leaves xs as [30, 19, 5], not [30, 19, 31] as Albert was suggesting.
Someone who's actually used the patch might correct me, but I'd be surprised if
the scoping rules were different to the rules for for loops.

	Cheers, Jesse.



More information about the Python-list mailing list