Op 26-11-15 om 16:36 schreef Marko Rauhamaa:
> Antoon Pardon :
>
>> [ for in ]
>>
>> would implicitly be rewritten as follows:
>>
>> [ (lambda : )() for in ]
>
> Funny enough, that's how "list comprehensions" are created in Scheme:
>
>(map (lambda (i)
> (lambda (x) (* i
Antoon Pardon writes:
> Op 26-11-15 om 14:56 schreef Marko Rauhamaa:
>> Antoon Pardon wrote:
>>
>>> I don't understand. What I propose would be a minor change in how
>>> list comprehension works. I don't see how your example can be turned
>>> into a list comprehension.
>>
>> The list comprehension
Antoon Pardon :
> [ for in ]
>
> would implicitly be rewritten as follows:
>
> [ (lambda : )() for in ]
Funny enough, that's how "list comprehensions" are created in Scheme:
(map (lambda (i)
(lambda (x) (* i x)))
'(0 1 2 3
> There would no change on how lamb
Op 26-11-15 om 14:56 schreef Marko Rauhamaa:
> Antoon Pardon :
>
>> I don't understand. What I propose would be a minor change in
>> how list comprehension works. I don't see how your example
>> can be turned into a list comprehension.
> The list comprehension is only a special case of the interact
Antoon Pardon :
> I don't understand. What I propose would be a minor change in
> how list comprehension works. I don't see how your example
> can be turned into a list comprehension.
The list comprehension is only a special case of the interaction between
closures and variables. If you dabble wi
Op 26-11-15 om 13:56 schreef Marko Rauhamaa:
> Antoon Pardon :
>
>> Personnaly I would prefer:
>>
> q = [(lambda i: lambda x: i * x)(i) for i in range(4)]
> q[0](1), q[3](1)
>> (0, 3)
>>
>> And this is where I ask whether it would be worth the effort to change
>> the behaviour of python.
>
Antoon Pardon :
> Personnaly I would prefer:
>
q = [(lambda i: lambda x: i * x)(i) for i in range(4)]
q[0](1), q[3](1)
> (0, 3)
>
> And this is where I ask whether it would be worth the effort to change
> the behaviour of python.
Don't go there.
Consider:
q = []
n = 0
x =
Op 26-11-15 om 12:13 schreef Nobody:
> Returning to the original expression:
>
> > q = [lambda x: i * x for i in range(4)]
> > q[0](1), q[3](1)
> (3, 3)
> > q = [lambda x,i=i: i * x for i in range(4)]
> > q[0](1), q[3](1)
> (0, 3)
Personnaly I would prefer:
>>>
On Wed, 25 Nov 2015 14:51:23 +0100, Antoon Pardon wrote:
> Am I missing something?
The issue is with lambdas rather than with list comprehensions per se.
Python's lambdas capture free variables by reference, not value.
> x = 3
> f = lambda y: x + y
> f(0)
3
Antoon Pardon writes:
> Op 20-11-15 om 08:49 schreef dieter:
>> In addition, the last few days have had two discussions in this list
>> demonstrating the conceptial difficulties of late binding -- one of them:
>>
>> Why does "[lambda x: i * x for i in range(4)]" gives
>> a list of ess
Op 20-11-15 om 08:49 schreef dieter:
> In addition, the last few days have had two discussions in this list
> demonstrating the conceptial difficulties of late binding -- one of them:
>
> Why does "[lambda x: i * x for i in range(4)]" gives
> a list of essentially the same functions?
C
11 matches
Mail list logo